Ruby undefined `encode 'method for String

I am trying to get this code to work (ruby 1.8.7):

line = "abc" "#{line}☃".encode('utf-8')[0..-2].scan(/\p{Katakana}/) 

but returns an undefined method 'encode' for "abc\342\230\203":String (NoMethodError) .

You can run the program here: http://codepad.org/nh6cAqHT

+7
source share
1 answer

You are probably using an older version of ruby. Available in version 1.9.3 but not in 1.8.7 , so check which version you are using.

 1.9.3p194 :001 > line = "abc" => "abc" 1.9.3p194 :002 > "#{line}☃".encode('utf-8')[0..-2].scan(/\p{Katakana}/) => [] 

It works great.

+10
source

All Articles