user12051965
user12051965

Reputation: 147

Is there a way to include elements from html inside a class in Ruby?

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.

Upvotes: 0

Views: 57

Answers (1)

Max
Max

Reputation: 22385

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)

Upvotes: 2

Related Questions