Passing inline hashes

B

Brock Weaver

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 =3D> 'blah', :class =3D> '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 =3D> 'blah'}, {:class =3D> '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 =3D> 'blah'}), ({:class =3D> '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 =3D 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?


--=20
Brock Weaver
 
J

Jamal Hansen

------=_Part_7134_8917456.1124235572002
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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).
=20
start_form_tag :action =3D> 'blah', :class =3D> 'css1'

I'm a Ruby Nuby as well, but why would you pass 2 hashs? Couldn't you pas=
s=20
a single hash?
start_form_tag { :action =3D> 'blah', :class =3D> 'css1' }
Maybe use parens if necessary
start_form_tag({ :action =3D> 'blah', :class =3D> 'css1' })
-Jamal

------=_Part_7134_8917456.1124235572002--
 
B

Brock Weaver

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.

=20
I'm a Ruby Nuby as well, but why would you pass 2 hashs? Couldn't you p= ass
a single hash?
start_form_tag { :action =3D> 'blah', :class =3D> 'css1' }
Maybe use parens if necessary
start_form_tag({ :action =3D> 'blah', :class =3D> 'css1' })
-Jamal
=20
=20


--=20
Brock Weaver
http://www.circaware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top