Passing inline hashes

Discussion in 'Ruby' started by Brock Weaver, Aug 16, 2005.

  1. Brock Weaver

    Brock Weaver Guest

    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
    Brock Weaver, Aug 16, 2005
    #1
    1. Advertising

  2. Brock Weaver wrote:

    >Any thoughts?
    >
    >

    # starts a line comment in Ruby.

    --
    Florian Frank
    Florian Frank, Aug 17, 2005
    #2
    1. Advertising

  3. Brock Weaver

    Brock Weaver Guest

    ...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 <> wrote:
    > Brock Weaver wrote:
    >=20
    > >Any thoughts?
    > >
    > >

    > # starts a line comment in Ruby.
    >=20
    > --
    > Florian Frank
    >=20
    >=20
    >=20



    --=20
    Brock Weaver
    http://www.circaware.com
    Brock Weaver, Aug 17, 2005
    #3
  4. Brock Weaver

    Jamal Hansen Guest

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

    On 8/16/05, Brock Weaver <> 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).
    >=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--
    Jamal Hansen, Aug 17, 2005
    #4
  5. Brock Weaver

    Brock Weaver Guest

    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 <> wrote:
    > On 8/16/05, Brock Weaver <> wrote:
    >=20
    > > 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'

    >=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
    Brock Weaver, Aug 17, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    543
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    151
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    255
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    204
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    192
Loading...

Share This Page