# Bug in ruby (1.8.7) hash: assignment executes code

**URL:** <https://www.ruby-forum.com/t/bug-in-ruby-1-8-7-hash-assignment-executes-code/158047>\
**Category:** Ruby\
**Created:** [January 18, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-1-8-7-hash-assignment-executes-code/158047 "2009-01-18T14:15:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saboteur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@saboteur](https://www.ruby-forum.com/u/saboteur)\
**Post date:** [January 18, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-1-8-7-hash-assignment-executes-code/158047/1 "2009-01-18T14:15:38Z")

</div>

Tested with: ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]

#!/usr/bin/env ruby

$VERBOSE = true

test\_hash=Hash[“value” =\> exit]  
puts “Never get’s executed.”

exit is executed and stops the application. Is this a wanted behavior?  
Shouldn’t an error be thrown like:  
NameError: undefined local variable or method `exit’ for  
main:Object

This bug can’t be abused for luck:

userinput=’%x[nc -lp 1234 -e /bin/bash]’  
test\_hash=Hash[“value” =\> userinput]

Doesn’t work because userinputs gets automatically threated like a  
string. Nevertheless I think this isn’t a good behavior or?

---

<div class="post-metadata">

**Author:** ![saboteur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@saboteur](https://www.ruby-forum.com/u/saboteur)\
**Post date:** [January 18, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-1-8-7-hash-assignment-executes-code/158047/2 "2009-01-18T15:33:20Z")

</div>

On Sunday 18 January 2009 14:13:50 removed\_email\_address@domain.invalid wrote:

> exit is executed and stops the application. Is this a wanted behavior?  
> Yes.

> Shouldn’t an error be thrown like: NameError: undefined local variable or  
> method `exit’ for  
> main:Object

But this function (exit) _does_ exist.

> Doesn’t work because userinputs gets automatically threated like a string.  
> Nevertheless I think this isn’t a good behavior or?

I think you misunderstood what that does. {X =\> Y} is equivalent to

h = {}  
h[X] = Y

that is, it assigns to hash result of expression Y. In your case the  
expression is calling function exit(), then assigning return value of  
the  
function to the hash. Since exit doesn’t return and terminates the  
program, it  
is kind of nonsensical statement, nevertheless correct. It’s the same  
as:

def current\_time  
return Time.now  
end

test\_hash = Hash[“value” =\> current\_time]

Jan
