Ruby reports undefined method `table' for CSV: Class (NoMethodError) error

Asked 2 years ago, Updated 2 years ago, 69 views

When executing the following code,
game.rb:3:undefined method `table' for CSV:Class(NoMethodError)
An error similar to this appears.

How can I deal with the error?
ruby file (game.rb)

require'csv'

csv = CSV.table ('data.csv')

time = csv[:time]# To specify a header

puts time.class#=>array
puts time#=>10:00
          #    20:00
          #    03:00

one = csv[1]# To specify a line
puts one.class#=>CSV::Row
put one#=>20:10, GOODNIGHT, miki
put one [:time]#=>20:10

csv file (data.csv)

time, text, user
10:00, Hello, tarou
20:10, GOODNIGHT, miki
03:00, SINTYOKUDAME, se

ruby csv

2022-09-30 11:21

1 Answers

The reason is that the Ruby version is out of date as discussed in the comments.
The code in question works with Ruby 1.9 or higher, but since the 1.9 series has also finished maintenance, I think it would be better to update it to at least 2.0 or higher.(Ideal for using the current latest version of 2.2.3)

As for the version upgrade method, recently the mainstream is to use rbenv (or RVM) to switch to any version.
I will link the official page of rbenv for your reference.

https://github.com/sstephenson/rbenv

If you search the Internet, you will find a lot of information in Japanese, so please refer to them and proceed as follows.


2022-09-30 11:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.