# Passing inline hashes

**URL:** <https://rubytalk.org/t/passing-inline-hashes/20216>\
**Category:** ruby-talk\
**Created:** [16 August 2005 22:54 UTC](https://rubytalk.org/t/passing-inline-hashes/20216 "2005-08-16T22:54:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [16 August 2005 22:54 UTC](https://rubytalk.org/t/passing-inline-hashes/20216/1 "2005-08-16T22:54:34Z")

</div>

I just stumbled across an intresting problem with passing two hashes  
to a method -- I was trying to pass inline hashes and ruby  
interpretted them as blocks. (Makes sense looking back).

start\_form\_tag :action =\> 'blah', :class =\> 'css1'

This call was putting both values into the first parameter of the  
method (a hash), and none in the second one. So I altered it:

start\_form\_tag {:action =\> 'blah'}, {:class =\> 'css1'}

Which of course ruby thinks the {} are blocks and pukes.

To explicitly tell ruby they are hashes, I had to wrap them with ().

start\_form\_tag ({:action =\> 'blah'}), ({:class =\> 'css1'})

Which seems awkward, as most languages use the {} syntax for inline hashes.

Since blocks can be do ... end or {}, I'd suggest we do something  
similar to the string parsing way: blocks declared with {} must be of  
the form #{}. This would clear up the hash/block problem, as well as  
make hashes look more like other languages. So:

"1234".each #{ |c| puts c }

(or something to that effect)

This would make sense to me, as the in-string replacement:

num\_of\_chars = 37  
puts "here's #{num\_of\_chars} chars to print"

seems to naturally flow as though the string object is passing control  
to the expression within #{}, just like a block would.

I'm definitely a ruby newbie, but it would be much more obvious to me  
this was a block vs. hash problem had that syntax been in place.

Any thoughts?

> **···**
>
> --  
> Brock Weaver

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [16 August 2005 23:06 UTC](https://rubytalk.org/t/passing-inline-hashes/20216/2 "2005-08-16T23:06:00Z")

</div>

Brock Weaver wrote:

> Any thoughts?

# starts a line comment in Ruby.

> **···**
>
> --  
> Florian Frank

---

<div class="post-metadata">

**Author:** ![Jamal\_Hansen](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Jamal\_Hansen](https://rubytalk.org/u/Jamal_Hansen)\
**Post date:** [16 August 2005 23:39 UTC](https://rubytalk.org/t/passing-inline-hashes/20216/3 "2005-08-16T23:39:36Z")

</div>

I'm a Ruby Nuby as well, but why would you pass 2 hashs? Couldn't you pass  
a single hash?  
start\_form\_tag { :action =\> 'blah', :class =\> 'css1' }  
Maybe use parens if necessary  
start\_form\_tag({ :action =\> 'blah', :class =\> 'css1' })  
-Jamal

> **···**
>
> On 8/16/05, Brock Weaver \<brockweaver@gmail.com\> wrote:
> 
> > I just stumbled across an intresting problem with passing two hashes  
> > to a method -- I was trying to pass inline hashes and ruby  
> > interpretted them as blocks. (Makes sense looking back).
> > 
> > start\_form\_tag :action =\> 'blah', :class =\> 'css1'

---

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [16 August 2005 23:31 UTC](https://rubytalk.org/t/passing-inline-hashes/20216/4 "2005-08-16T23:31:07Z")

</div>

...but # is escaped within a string, so there is an exception. #{  
would start a block,  
while a #\<space\>{ would be a comment.

> **···**
>
> On 8/16/05, Florian Frank \<flori@nixe.ping.de\> wrote:
> 
> > Brock Weaver wrote:
> > 
> > \>Any thoughts?  
> > \>  
> > \>  
> > # starts a line comment in Ruby.
> > 
> > --  
> > Florian Frank
> 
> --  
> Brock Weaver  
> [http://www.circaware.com](http://www.circaware.com)

---

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [16 August 2005 23:58 UTC](https://rubytalk.org/t/passing-inline-hashes/20216/5 "2005-08-16T23:58:05Z")

</div>

Sure you can, but the start\_form\_tag method expects the first hash to  
contain url data, and the second hash to contain "options" -- html  
attributes.

> **···**
>
> On 8/16/05, Jamal Hansen \<jamal.hansen@gmail.com\> wrote:
> 
> > On 8/16/05, Brock Weaver \<brockweaver@gmail.com\> wrote:
> > 
> > \> I just stumbled across an intresting problem with passing two hashes  
> > \> to a method -- I was trying to pass inline hashes and ruby  
> > \> interpretted them as blocks. (Makes sense looking back).  
> > \>  
> > \> start\_form\_tag :action =\> 'blah', :class =\> 'css1'
> > 
> > &nbsp;&nbsp;I'm a Ruby Nuby as well, but why would you pass 2 hashs? Couldn't you pass  
> > a single hash?  
> > start\_form\_tag { :action =\> 'blah', :class =\> 'css1' }  
> > Maybe use parens if necessary  
> > start\_form\_tag({ :action =\> 'blah', :class =\> 'css1' })  
> > -Jamal
> 
> --  
> Brock Weaver  
> [http://www.circaware.com](http://www.circaware.com)
