Panagiotis Panagi
Panagiotis Panagi

Reputation: 10087

Ruby undefined method `encode' for String

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

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

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

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

Upvotes: 7

Views: 5205

Answers (1)

AnandVeeramani
AnandVeeramani

Reputation: 1556

You are probably using an older version of ruby. It is available in 1.9.3 but not in 1.8.7, so check which version you're using.

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

Works fine.

Upvotes: 10

Related Questions