Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Ruby comes with a built-in REPL, which is quite handy.

screenshot of IRB

Your challenge is to crash it in the least amount of code!

The definition of "crash" is "make it exit in an unintended way." This means exit, quit, abort, irb_exit, irb_quit, et. al. are not valid answers.

Furthermore, you may not cause any side-effect to any other part of the system. For example, `rm -rf /` is not valid either.

Any version 1.9.3 or above is valid. If your code only works on a specific version of Ruby, you may specify that in the answer.

The final restriction is that you may not rely on any gems.

This is , so the shortest code that crashes IRB will win!

share|improve this question
    
@ace Hmm, that does seem to qualify as "unintended" so yes. –  Doorknob 冰 Feb 27 at 14:27
    
I can haz golfscript anser, plz? –  DigitalTrauma Feb 27 at 15:33
1  
wow. i don't know ruby, and after reading these answers i'll never learn it. –  izabera Feb 27 at 19:36

7 Answers 7

up vote 5 down vote accepted

5 characters

ENV=0

(inspired by @daniero's answer)

share|improve this answer
    
doesn't work for me: irb(main):001:0> ENV=0 (irb):1: warning: already initialized constant ENV –  Brian Minton Feb 27 at 21:19
    
@BrianMinton Had only a 1.9.3p194 at hand and it "works" with that version. –  Howard Feb 27 at 21:27
    
ah, my version is quite old: irb 0.9.6(09/06/30) –  Brian Minton Feb 27 at 21:36
    
@Brian Woah, that's ancient :-O –  Doorknob 冰 Feb 27 at 23:49
1  
@BrianMinton that is the latest version of irb –  user17752 Mar 2 at 10:32

16 characters

String=0
String=0

Not the shortest, but I think it's funny that it doesn't crash until the second line. Generates roughly 20 lines of text before IRB exits. For some reason it cannot be shortened to for instance 2.times{String=0}.


edit

Of all the answers so far, this is the only one that has worked for me (and it works in all versions I could get my hands on), and I've tested all of them in these versions:

On whatever kind of Linux I get when ssh'ing into my university:
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
ruby 1.8.5 (2006-08-25) [x86_64-linux]
Mac OS X Mavericks default:
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
Installed through Homebrew on OS X:
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]

edit 2

7 characters

Combining my first version (and/or @Howard's answer, for maximum cross reference) with @chinese perl goth's answer:

STDIN=0
share|improve this answer
    
Array=0 crashes immediately. –  primo Feb 27 at 16:46
1  
@primo cool, but I can't get it to work :/ (See my edit) –  daniero Feb 27 at 18:33
    
Same here, only this and the closing stdin answer work, all the others give warnings only. –  ace Feb 27 at 20:47
    
Yes, the stdin one works for me too. It was posted after my edit. –  daniero Feb 27 at 20:56
    
You can shorten the second line to = and it still crashes. –  Fraxtil Feb 27 at 21:50

12 chars

ruby is not exactly my cup of tea, but I've just found out that irb acts funny when I close the stdin :)

$stdin.close

tested on irb 0.9.6(09/06/30) and ruby 1.9.3p194

share|improve this answer
4  
STDIN.close works too, and it's 1 less char! –  Kavu Feb 27 at 19:57
    
+1 because it's so obvious when you look at it that it will cause trouble :) My answer is probably more of a bug, but for this one you can't really expect IRB to do anything clever; it's like in Star Trek or whatever when they ask a super intelligent robot an impossible question and it explodes. –  daniero Feb 27 at 20:55

22 characters

def method_missing;end

Apparently it messes with some irb internals. (To fix it, add self. after def.)

share|improve this answer

10 9 chars

A shorter variant on @daniero's answer:

String=1
-

This works at least in the default OS X Mavericks Ruby (2.0.0).

The answer basically relies on the fact that the Ruby Token function does a case on the input token. One of the cases checks against String, which has been redefined by the first line. This case fails, so the case falls through to the default, which assumes the object has an ancestors accessor (which it does not).

Because the "bug" is in the tokenizer, the first line won't fail because the line only takes effect after the parsing is finished. Thus, it only affects subsequent lines. Subsequent lines must contain some kind of operator in order to see the failure.

share|improve this answer
    
+1 For the explanation. But who is this daneiro? ;) –  daniero Feb 28 at 1:27
    
@daniero: My apologies, I appear to be lysdexic today. –  nneonneo Feb 28 at 1:36

12 characters

def send;end

As far as I know, there are four methods in the Object class which show this kind of behaviour:

send
method_missing
respond_to?
respond_to_missing?
share|improve this answer
4  
In my IRB it simply returns nil and carries on –  daniero Feb 27 at 16:24
    
@daniero 1.8.6 and 1.9.3 both crash. –  primo Feb 27 at 16:38
    
@primo no longer crashes on 2.0.0 –  drusepth Mar 28 at 12:25

5 Characters

IRB=0

Nothing disturbs IRB quite like redefining IRB.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.