On Dec 24, 4:59=A0am, Robert Klemme <shortcut... / googlemail.com> wrote:
> On 12/24/2009 08:05 AM, Philliam Auriemma wrote:
>
> > Hey all,
> > I'm pretty new to ruby, and I absolutely love it. I have aquestion. I
> > have programmed in a language called lua before (which had real full-
> > fledged anonymous functions), and with a lua table, which is Lias
> > version of a hash and an arrary combined, you could do something like
>
> Just out of curiosity: where is the array in your Lua case? =A0In Ruby yo=
u
> would not need an Array for this - or rather, you need either an Array
> or a Hash.
>
> > this:
>
> > newtable =3D { 1 =3D function() print("hi from 1") end,
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A02 =3D function() print("hi from =
2") end
> > }
>
> > even though some of you haven't even ever heard of lua before, it's
> > easy to guess what this does: it creates two keys, 1 and 2, in a
> > table, newtable, and assigns two functions as their values. After
> > doing that, you can do this:
>
> > newtable[1]()
>
> > To call the function stored in newtable[1]. All that begs the
> > question: is something similar available in ruby?
>
> Yes. =A0(I'm inclined to say "Of course!" :-)) =A0Please see Bill's
> explanation.
>
> Additional note: since the advent of Ruby 1.9 you can also use another
> syntax for anonymous functions:
>
> f =3D ->(args) { code }
>
> newtable =3D {
> =A0 =A01 =3D> -> { puts "hi from 1" },
> =A0 =A02 =3D> -> { puts "hi from 2" },
>
> }
>
> If you only have numeric indexes in sequence you can also use an Array
> instead of a Hash:
>
> newtable =3D [
> =A0 =A0-> {}, # index 0
> =A0 =A0-> { puts "hi from 1" },
> =A0 =A0-> { puts "hi from 2" },
> ]
>
> Kind regards
>
> =A0 =A0 =A0 =A0 robert
>
> --
> remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestp=
ractices.com/

Holy crap that's awesome. Thanks for telling me about the new syntax
for anonymous function Rob, it just makes me love ruby even more now.
Thanks again guys.