Syntax error I am getting while csv operations

Hi,

I am keep getting an error from the below code.

hash = { ‘A’ => [ ‘v’, ‘x’, ‘y’ , ‘z’ ] , ‘B’ => [ ‘m’, ‘n’ , ‘o’ ] }
CSV.open (’/home/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) do
|csv|
hash.each do |k, v|
csv[k]=v
end
end

~> -:2: syntax error, unexpected ‘,’, expecting ‘)’

~> …me/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) |csv|

~> … ^

~> -:2: syntax error, unexpected ‘,’, expecting end-of-input

~> …irti/workspace/Ruby/a.csv’,“w”, :headers => true) |csv|

~> … ^

Can anyone tell me where wrong I did ?

“CSV.open (” <<< remove the " "

Hans M. wrote in post #1134441:

“CSV.open (” <<< remove the " "

I don’t understand :frowning:

Hans M. wrote in post #1134449:

change
CSV.open (’/home/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) do
to
CSV.open(’/home/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) do

Thanks friend… Lots of code today… My eyes were not working at that
time… So… :slight_smile:

change
CSV.open (’/home/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) do
to
CSV.open(’/home/kirti/workspace/Ruby/a.csv’,“w”, :headers => true) do

On Jan 26, 2014, at 12:46 PM, Arup R. [email protected] wrote:

Hans M. wrote in post #1134441:

“CSV.open (” <<< remove the " "

I don’t understand :frowning:

I believe Hans means that you need to remove the space between CSV.open
and the opening parenthesis. The space there is significant to Ruby:

~ ∙ irb --simple-prompt

handle = File.open (’.bash_profile’, ‘rb’)
SyntaxError: (irb):1: syntax error, unexpected ‘,’, expecting ‘)’
handle = File.open (’.bash_profile’, ‘rb’)
^
(irb):1: syntax error, unexpected ‘)’, expecting end-of-input
from /Users/mike/.rubies/ruby-2.1.0/bin/irb:11:in `’

handle = File.open(’.bash_profile’, ‘rb’)
=> #<File:.bash_profile>

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Mike S. wrote in post #1134452:

On Jan 26, 2014, at 12:46 PM, Arup R. [email protected] wrote:

Hans M. wrote in post #1134441:

“CSV.open (” <<< remove the " "

Yes, Mike that worked. Thanks for your reply. I got it from Hans’s
second reply although.

handle = File.open(’.bash_profile’, ‘rb’)
=> #<File:.bash_profile>

Hope this helps,

Mike

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs