I'm converting a csv file to an xml with the code below. This works
fine. Maybe it can be done differently and better but it works for me.
I have three questions:
1 - Why cant I re-use "csv" within "builder.type" ? If re-use it I dont
get any data.
2 - I would like to add an attribute to builder.type. How can do that? I
want the result to be like <type upgnr="12345"> and then the rest of the
xml..
3 - If I change to an excel file as source. How should I change the
code?
Any suggestions are appreciated. Maybe my code is not so "ruby"? Please
show me an example of how I can improve
Making the variable something like @csv or $csv should make it visible
inside the block, since those are not local variables (instance and
global, respectively).
I suspect that builder.type doesn't work because you've commented out
the rest of that line:
builder.type{#, "upgnr"=>"12345"
Here the "#" makes the rest of the line a comment, and is ignored by the
interpreter.
Regarding the variables I have tried @csv and $csv without luck.
In my code above I forgot to delete the outcommented code after the #.
Even if I remove the # it doesnt work. At the moment (with the out
commented line above) the result is: <type> without the attribute
"upgnr".
Well...this is the error with suggested inspect and length..
C:\CR - XML convevert>ruby converter_test.rb
converter_test.rb:31:in `block (2 levels) in <main>' undefined method
'length'
for #<CSV:0x2396bc8> (NoMethodError)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems
se.rb:170:in `call'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems
se.rb:170:in `_nested_structures'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems
se.rb:63:in `tag!'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems
se.rb:88:in `method_missing'
from converter_test.rb:29:in `block in <main>'
from converter_test.rb:19:in `open'
from converter_test.rb:19:in `<main>'
You should be able to, yes. There shouldn't be any difference between
using the variable and reopening the file, as far as I can see, except
possibly a conflict with opening the file when it's already open.
At this point you should continue entering debugging lines, like this:
Perhaps "readlines" leaves nothing left to iterate through with "each".
I suspect that the csv keeps its current position, meaning you're
already at the end of the file after using "readlines".
On 7 Οκτ 2013, at 16:47 , Χαράλαμπος Κώστας <root@charkost.gr> wrote:
--
The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."