tags:

views:

100

answers:

3

I'm a bit confused with the '£' symbol in Ruby.

In JRuby if I do :

puts '£40'

in a .rb file I run this, I get

£40

In JRuby IRB I get :

>> pung = 'h40'
=> "h40"
>> pung.gsub!('h', '£')
pung.gsub!('h', '£')
=> "\24340"

The pound symbol is output as \243.

In pure Ruby IRB, I cant even enter the £ symbol.. The cursor jumps to the left three spaces when I hit the £ key!

trying .toutf8 or toutf16 bring up even stranger characters!

Whats going on!??!? Why cant I just output a simple £?

A: 

Most likely, the symbol is a Unicode symbol and you are converting it (perhaps unintentionally). If you can't enter the pound sterling symbol, make sure your console supports Unicode.

What do you get when you do £.class ? String? Unicode::String? Perhaps explicitly declaring the character as a Unicode::String or Unicode::Character will give different results.

bta
If I do £.class I get : '\243Invalid char `\243' ('¬£') in expression (SyntaxError)'.. If I do '£'.class I get String. Maybe a stupid question.. how do I declare as Unicode:String?
Mongus Pong
Make sure you `require 'unicode'` first, then do `"£".to_u`. See http://ruby-unicode.rubyforge.org/doc/ for more details. If your terminal is not letting you enter Unicode characters directly, try `"\243".to_u`.
bta
A: 

'\243' is the octal escape sequence for '£'.

dan04
+1  A: 

Sometimes this is a problem with the way your console pastes the character. For example, the unicode character sequence may include a character the console uses to do backspace or arrow left. This is probably the issue with the IRB console not receiving your character ok.

For the script, it looks like JRuby's doing what it's supposed to. The issue with the console should probably be reported as a bug, however, since we do want IRB to support entering unicode characters. Pop over to JRuby's bug tracker at http://bugs.jruby.org and provide show a simple session or provide steps to reproduce (which should be easy).

Charles Oliver Nutter
The console problems are with normal Ruby, not JRuby. JRuby does cooperate a lot better! The problem I cant work out with JRuby is what is with this extra ¬ character. This doesnt show up with JRuby IRB.. but does if I am running a .rb file through JRuby in Netbeans. Its puzzling...
Mongus Pong