Array#inject with hash as initial, unexpected error

Discussion in 'Ruby' started by Matthew Moss, Mar 13, 2006.

  1. Matthew Moss

    Matthew Moss Guest

    (On Mac OS X 10.4.5, Ruby 1.8.4)

    Okay, so:

    > [1, 2, 3].inject(0) { |s, v| s +=3D v }

    =3D> 6

    and:

    > [1, 2, 3].inject([]) { |a, v| a << v**2 }

    =3D> [1, 4, 9]

    but:

    > [1, 2, 3].inject({}) { |h, v| h[v] =3D v**2 }

    =3D> NoMethodError: undefined method `[]=3D' for 1:Fixnum


    What gives?
    I've tried replacing {} with Hash.new and a couple other variants without l=
    uck.
    Matthew Moss, Mar 13, 2006
    #1
    1. Advertising

  2. Hey Matthew,

    observe:

    irb(main):001:0> [] << 100
    => [100]
    irb(main):002:0> {}[:something] = 100
    => 100

    The << operator on an array returns the array, while the [] operator
    on a hash returns the value you assigned.

    And the result of the last statement in your inject block will
    replace the value for your memo on each iteration.

    So... just make sure the last statement in your inject block is the
    memo value you are accumulating:

    [1,2,3].inject({}) { |memo, val| memo[val] = val**2; memo}

    HTH,
    Trevor
    --
    Trevor Squires
    http://somethinglearned.com



    On 12-Mar-06, at 5:45 PM, Matthew Moss wrote:

    > (On Mac OS X 10.4.5, Ruby 1.8.4)
    >
    > Okay, so:
    >
    >> [1, 2, 3].inject(0) { |s, v| s += v }

    > => 6
    >
    > and:
    >
    >> [1, 2, 3].inject([]) { |a, v| a << v**2 }

    > => [1, 4, 9]
    >
    > but:
    >
    >> [1, 2, 3].inject({}) { |h, v| h[v] = v**2 }

    > => NoMethodError: undefined method `[]=' for 1:Fixnum
    >
    >
    > What gives?
    > I've tried replacing {} with Hash.new and a couple other variants
    > without luck.
    >
    Trevor Squires, Mar 13, 2006
    #2
    1. Advertising

  3. Matthew Moss

    Matthew Moss Guest

    Okay, I feel silly now. Thanks for the reminder on basic assignment
    protocol. =3D)


    On 3/12/06, Trevor Squires <> wrote:
    > Hey Matthew,
    >
    > observe:
    >
    > irb(main):001:0> [] << 100
    > =3D> [100]
    > irb(main):002:0> {}[:something] =3D 100
    > =3D> 100
    >
    > The << operator on an array returns the array, while the [] operator
    > on a hash returns the value you assigned.
    >
    > And the result of the last statement in your inject block will
    > replace the value for your memo on each iteration.
    >
    > So... just make sure the last statement in your inject block is the
    > memo value you are accumulating:
    >
    > [1,2,3].inject({}) { |memo, val| memo[val] =3D val**2; memo}
    Matthew Moss, Mar 13, 2006
    #3
    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. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  2. Peña, Botp

    inject does not inject last value

    Peña, Botp, Aug 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    178
    Peña, Botp
    Aug 7, 2006
  3. Anthony Martinez
    Replies:
    4
    Views:
    268
    Robert Klemme
    Jun 11, 2007
  4. Michal Suchanek
    Replies:
    6
    Views:
    226
    Nobuyoshi Nakada
    Jun 13, 2007
  5. Alex Fort

    Unexpected behavior in inject

    Alex Fort, Jun 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    135
    Alex Fort
    Jun 15, 2010
Loading...

Share This Page