Problem on an example from ruby-lang.org

  • Thread starter Andre Willik valenti
  • Start date
A

Andre Willik valenti

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 method `force_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:in
`each'
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:in
`rl_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:in
`readline'
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:in `gets'
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:in `signal_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:in `call'
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:in `getc'
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:in `match'
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:in `lex'
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:in `loop'
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:in `catch'
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:in `eval_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:in `catch'
from C:/Ruby192/lib/ruby/1.9.1/irb.rb:69:in `start'
from C:/Ruby192/bin/irb:12:in `<main>'


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
 
B

Brian Candler

Andre said:
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? :)

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top