Computer Science Canada

Ruby is pretty cool, but tutorial lied.

Author:  mirhagk [ Sat Jan 01, 2011 5:58 pm ]
Post subject:  Ruby is pretty cool, but tutorial lied.

Ruby's get started in 20 minutes tutorial says this is possible

Ruby:

class Fixnum
  # You can, but please don't do this
  def +( other )
    self - other
  end
end


but if I try to do it, the interactive ruby epic fails and closes. Why is this?

Author:  Tony [ Sat Jan 01, 2011 6:05 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

works just fine
code:

irb(main):001:0> class Fixnum
irb(main):002:1>   def +(other)
irb(main):003:2>     self - other
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0>
irb(main):005:0* 2 + 2
=> 0

Author:  mirhagk [ Sat Jan 01, 2011 10:58 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

after defining the class it returns nil, then freaks out with something about method not found, then 30 lines of from C:\ruby192\lib\ruby\.... etc
then quits.

Author:  Tony [ Sat Jan 01, 2011 11:26 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

I can't tell you much without you being more specific about that method and those lines.

Author:  mirhagk [ Sat Jan 01, 2011 11:55 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

=> nil
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_p
rompt'
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1765:in `rl_expan
d_prompt'
from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rbreadline.rb:1790:in `rl_set_p
rompt'
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_i
nput'
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_st
atement'
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>'

thats the response, then im back at the command line (after ruby exits)

Author:  Tony [ Sun Jan 02, 2011 12:10 am ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

Maybe try with --noreadline flag on irb?

Author:  mirhagk [ Sun Jan 02, 2011 6:46 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

k i tried to redefine the -, and it quit, this time it said stack level too deep


EDIT: and how do I do the noreadline thing?

Author:  jcollins1991 [ Sun Jan 02, 2011 6:54 pm ]
Post subject:  Re: Ruby is pretty cool, but tutorial lied.

code:
irb --noreadline

Author:  mirhagk [ Sun Jan 02, 2011 9:27 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

thank you, that worked, can you explain what the noreadline thing does?

Author:  Tony [ Sun Jan 02, 2011 11:19 pm ]
Post subject:  RE:Ruby is pretty cool, but tutorial lied.

It disables readline, which is where the fatal exception seems to occur. Readline allows for in-line editing such as tab completion and history.


: