Passing inline hashes

(Brock Weaver) #1

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

(Florian Frank) #2

Brock Weaver wrote:

Any thoughts?

# starts a line comment in Ruby.

···

--
Florian Frank

(Jamal Hansen) #3

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'

(Brock Weaver) #4

...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

(Brock Weaver) #5

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'

  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