0

I am working on a web scraping problem. I selected elements from HTML using css-selectors and I was wondering if it is possible to include them inside a class in Ruby:

name = browser.p(css: 'p[bo-bind="row.zName"]').text

account = browser.span(css: 'span[bo-bind="row.zAccount"]').text

Is there a way to declare name and account fields inside a class Accounts?

Something like this:

class Accounts @@name @@account

and then assign to those elements from HTML.

user12051965
  • 97
  • 9
  • 29
  • Possible duplicate of [Linking Ruby with HTML Code](https://stackoverflow.com/questions/34056486/linking-ruby-with-html-code) – olminkow Sep 12 '19 at 19:16

1 Answers1

2

If your code looks like this

name = browser.p(css: 'p[bo-bind="row.zName"]').text
account = browser.span(css: 'span[bo-bind="row.zAccount"]').text

You can encapsulate that in a class like so

class Account
  def initialize browser
    @name = browser.p(css: 'p[bo-bind="row.zName"]').text
    @account = browser.span(css: 'span[bo-bind="row.zAccount"]').text
  end
end

account = Account.new(browser)
Max
  • 21,123
  • 5
  • 49
  • 71