素人がプログラミングを勉強していたブログ

プログラミング、セキュリティ、英語、Webなどのブログ since 2008

連絡先: すかいぷ:javascripter_  か javascripter あっと tsukkun.net skypeのほうがいいです

NilClassのnil?を再定義するとRuby(irb)が壊れる

Rubyじゃなくてirbの問題だった。コメント参照。

class NilClass;def nil?;false;end;end

して、

nil.hoge

とすると、落ちる。

% ruby -v                                                                                                 [~]
ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-darwin9]
% echo $OSTYPE                                                                                            [~]
darwin9.0% irb                                                                                                     [~]
irb(main):001:0> class NilClass;def nil?;false;end;end
=> 
irb(main):002:0> nil.hoge
/opt/local/lib/ruby/1.8/irb/slex.rb:235:in `match_io': undefined method `call' for nil:NilClass (NoMethodError)
        from /opt/local/lib/ruby/1.8/irb/slex.rb:76:in `match'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:287:in `token'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:263:in `lex'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:234:in `each_top_level_statement'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in `loop'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in `each_top_level_statement'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in `catch'
        from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in `each_top_level_statement'
        from /opt/local/lib/ruby/1.8/irb.rb:146:in `eval_input'
        from /opt/local/lib/ruby/1.8/irb.rb:70:in `start'
        from /opt/local/lib/ruby/1.8/irb.rb:69:in `catch'
        from /opt/local/lib/ruby/1.8/irb.rb:69:in `start'
        from /opt/local/bin/irb:13
%