AhmetMelihBaşbuğ AhmetMelihBaşbuğ - 1 month ago 9
Ruby Question

In Ruby, each_hash function syntax error, unexpected tIDENTIFIER, expecting keyword

I try to connect my database with ruby in that example and I want to put output name, surname from members table. However i couldn't show output as

name surname
. It has error. I am new in ruby. How can use
each_hash
in that example? Output:

name surname
name1 surname1


my code:

con = Mysql.new('localhost', 'root', 'root', 'ruby')
rs = con.query('select * from members')
rs.each_hash { |name, surname| puts name['name'], puts surname['surname']}


error:


$> ruby membershow.rb

membershow.rb:7: syntax error, unexpected tIDENTIFIER, expecting
keyword_do or '{' or '(' ...puts name['name'], puts
surname['surname']}


Thanks

Answer

You can not use , to separate lines or end line. It should be ;. use this:

rs.each_hash { |name, surname| puts name['name']; puts surname['surname']}