i’ve written a function within a model to scrape a site and store certain attributes within a separate model (story):
def get_content
request = HTTParty.get("#{url}")
doc = Nokogiri::HTML(request.body)
doc.css("#{anchor}")["#{range}"].each do |entry|
story = self.stories.new
story.title = entry.text
story.url = entry[:href]
story.save
end
This uses the url, anchor, and range attributes of a Sections variable. The range attribute is stored as an array range – i.e. 0..2 or 11..13 – however, I’m being told that it can’t convert a string into a variable. I’ve tried storing range as an integer and as a string, but both fail.
I realise I could input the beginning and end of the range as two separate integers in my db, and put ["#{beginrange}".."#{endrange}"] but this seems a messy way of doing it.
Any other ideas? Many thanks in advance
Hmm if you are sure that the
rangeis always a string like ‘1..2‘ (‘<Integer >..<Integer>‘), you can use theevalmethod:In my IRB console:
In your case:
But
evalis kind of dangerous. If you are sure that therangeattribute is a Range as a String (validations and Regex are here to help), you can useevalwithout risk.