Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby undefined method `encode' for String

Tags:

ruby

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

like image 747
Panagiotis Panagi Avatar asked Oct 27 '12 20:10

Panagiotis Panagi


1 Answers

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.

like image 178
AnandVeeramani Avatar answered Oct 21 '22 01:10

AnandVeeramani