Problem on an example from ruby-lang.org

Hello all,

I’m new to Ruby and I’ve been taking a look at the “Getting Started”
stuff from ruby-lang.org. I found the following example from
http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/:

class Fixnum

You can, but please don’t do this

def +( other )
self - other
end
end

If I run this code using the ruby command, it’s fine. However, on irb, I
get the following error:

C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:8446:in
_rl_find_next_mbchar': undefined methodforce_encoding’ for
nil:NilClass (NoMethodError)
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1692:in
block in expand_prompt' from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1676:ineach’
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1676:in
expand_prompt' from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1765:inrl_expand_prompt’
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1790:in
rl_set_prompt' from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:4718:inreadline’
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/readline.rb:40:in
readline' from C:/Ruby192/lib/ruby/1.9.1/irb/input-method.rb:115:ingets’
from C:/Ruby192/lib/ruby/1.9.1/irb.rb:139:in block (2 levels) in eval_input' from C:/Ruby192/lib/ruby/1.9.1/irb.rb:273:insignal_status’
from C:/Ruby192/lib/ruby/1.9.1/irb.rb:138:in block in eval_input' from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:188:incall’
from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:188:in
buf_input' from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:103:ingetc’
from C:/Ruby192/lib/ruby/1.9.1/irb/slex.rb:205:in match_io' from C:/Ruby192/lib/ruby/1.9.1/irb/slex.rb:75:inmatch’
from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:286:in token' from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:262:inlex’
from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:233:in block (2 levels) in each_top_level_statement' from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:229:inloop’
from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in block in each_top_level_statement' from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:228:incatch’
from C:/Ruby192/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in
each_top_level_statement' from C:/Ruby192/lib/ruby/1.9.1/irb.rb:155:ineval_input’
from C:/Ruby192/lib/ruby/1.9.1/irb.rb:70:in block in start' from C:/Ruby192/lib/ruby/1.9.1/irb.rb:69:incatch’
from C:/Ruby192/lib/ruby/1.9.1/irb.rb:69:in start' from C:/Ruby192/bin/irb:12:in

And then irb abruptly closes. I’m using Windows 7 Professional, ruby
1.9.2p0 (2010-08-18) [i386-mingw32] and irb 0.9.6(09/06/30).

I’ve done some searching about this problem, but couldn’t find anything.
I apologize in case it is a known issue.

Regards,
Andre

Andre Willik valenti wrote:

class Fixnum

You can, but please don’t do this

def +( other )
self - other
end
end

Which bit of “please don’t do this” don’t you understand? :slight_smile:

irb is itself a large and quite complex ruby program; look in
/usr/lib/ruby/1.8/irb/* or the equivalent on your system.

If you break basic methods like addition, then it will fail. The same
would apply to pretty much any other ruby program.

It is, however, a good demonstration of why polluting core classes is a
really bad idea (because other people’s code will depend on them
behaving in the usual way, and you can’t isolate those changes so they
only affect your code)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs