Crash IRB (interactive Ruby)

18

2

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!

Doorknob

Posted 2014-02-27T14:05:15.760

Reputation: 68 138

"The definition of 'crash' is 'make it exit in an unintended way.'" How are we supposed to write code intended to do something unintended? I vote to close as unclear what you're asking. – msh210 – 2016-07-21T15:06:33.487

I can haz golfscript anser, plz? – Digital Trauma – 2014-02-27T15:33:21.827

3wow. i don't know ruby, and after reading these answers i'll never learn it. – izabera – 2014-02-27T19:36:59.993

Answers

6

5 characters

ENV=0

(inspired by @daniero's answer)

Howard

Posted 2014-02-27T14:05:15.760

Reputation: 23 109

doesn't work for me: irb(main):001:0> ENV=0 (irb):1: warning: already initialized constant ENV – Brian Minton – 2014-02-27T21:19:38.510

@BrianMinton Had only a 1.9.3p194 at hand and it "works" with that version. – Howard – 2014-02-27T21:27:13.547

ah, my version is quite old: irb 0.9.6(09/06/30) – Brian Minton – 2014-02-27T21:36:27.153

1@BrianMinton that is the latest version of irb – DarkHeart – 2014-03-02T10:32:39.490

17

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

daniero

Posted 2014-02-27T14:05:15.760

Reputation: 17 193

Array=0 crashes immediately. – primo – 2014-02-27T16:46:52.910

1@primo cool, but I can't get it to work :/ (See my edit) – daniero – 2014-02-27T18:33:25.980

Same here, only this and the closing stdin answer work, all the others give warnings only. – user12205 – 2014-02-27T20:47:23.267

Yes, the stdin one works for me too. It was posted after my edit. – daniero – 2014-02-27T20:56:28.640

1You can shorten the second line to = and it still crashes. – Fraxtil – 2014-02-27T21:50:06.893

10

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

chinese perl goth

Posted 2014-02-27T14:05:15.760

Reputation: 1 089

Related: $>.close. – Jordan – 2016-07-21T15:14:38.137

5STDIN.close works too, and it's 1 less char! – Kavu – 2014-02-27T19:57:00.567

+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 – 2014-02-27T20:55:38.040

7

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.

nneonneo

Posted 2014-02-27T14:05:15.760

Reputation: 11 445

+1 For the explanation. But who is this daneiro? ;) – daniero – 2014-02-28T01:27:13.523

@daniero: My apologies, I appear to be lysdexic today. – nneonneo – 2014-02-28T01:36:27.243

6

5 characters

$>=$<

Sets stdout to stdin which throws an error trying to open stdin for writing and crashes irb.

photoionized

Posted 2014-02-27T14:05:15.760

Reputation: 191

Note that this is tested on ruby 2.3.0p0 – photoionized – 2016-04-29T20:25:03.363

5

22 characters

def method_missing;end

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

Doorknob

Posted 2014-02-27T14:05:15.760

Reputation: 68 138

4

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?

David Herrmann

Posted 2014-02-27T14:05:15.760

Reputation: 1 544

4In my IRB it simply returns nil and carries on – daniero – 2014-02-27T16:24:10.913

@daniero 1.8.6 and 1.9.3 both crash. – primo – 2014-02-27T16:38:30.813

@primo no longer crashes on 2.0.0 – drusepth – 2014-03-28T12:25:40.303

4

5 Characters

IRB=0

Nothing disturbs IRB quite like redefining IRB.

vgoff

Posted 2014-02-27T14:05:15.760

Reputation: 141

Yeah! This doesn't crash, but prints 20 lines errors after anything! – Mega Man – 2016-06-25T08:37:04.367

Probably a different version of IRB now, @MegaMan so by now, definitely YMMV. – vgoff – 2016-06-26T05:20:35.920

Both are cool, anyway! – Mega Man – 2016-06-26T13:44:30.150

2

12 10 characters

exec"exec"

I don't know if this counts, because of the exec

CocoaBean

Posted 2014-02-27T14:05:15.760

Reputation: 309

1You can get rid of the space after the first exec – Fund Monica's Lawsuit – 2016-04-29T20:31:49.323

2

8 characters

Similar to chinese perl goth's answer:

$>.close

$> is an alias for STDOUT.

Jordan

Posted 2014-02-27T14:05:15.760

Reputation: 5 001

0

26 + 1 = 27 characters

This isn't very golfy, but I was amused to discover it by accident and thought others might enjoy it.

class Fixnum;def +;end end

I added +1 to the score because you have to press Enter a second time after entering the above (but not +2 because no one else counted Enter).

Jordan

Posted 2014-02-27T14:05:15.760

Reputation: 5 001