Its very strange whats happening, Im making a loop to change an html and need to change the variable called product01, the code:
(10..34).each do |i|
li = @doc.css('li')[i-1]
li.content = @site.send("product0#{i-9}")
end
In the code above, the error says: undefined method `product010' for...
Then I just removed the 0:
(10..34).each do |i|
li = @doc.css('li')[i-1]
li.content = @site.send("product#{i-9}")
end
And the error now says: are undefined method `product1' for...
Ive already tried
@site.send("product"+"0"+"#{i-9}")
> product010
@site.send("product0".to_s+"#{i-9}")
> product010
@site.send("product"+"0#{i-9}"
> product010
So, I just want the product01.
To make things more clear, Im using nokogiri to open an html and change values via db, into the db the variables are called product01, product02, etc.
I can say @site.product01 but theres a lot of products so I want to make a loop instead one by one.
Its not an array of products, each product comes from one @site.
Yes, it works when |i| are higher than 10 but not from 0 to 9 because I need to put the 0.
@doc = Nokogiri::HTML(open("/home/file.html")
To make it work Ill remove all variables called 01,02..09 and change their names to 1,2,..9 if it wont work.
Thanks.
I'm not sure why you want product01 to product34, that looks wrong.
Is it possible to structure it as a 'products' array? That would be a lot more standard.
However, you can format a number to a given number of digits...
irb> '%02d' % 6
=> "06"
This would seem to solve the problem.
(taken from How to make single digit number a two digit number in ruby?)