Reputation: 147
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
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