Why won't this lambda thing work?

10 views
Skip to first unread message

ms

unread,
Apr 20, 2012, 6:37:14 PM4/20/12
to Ruby on Rails: Talk
Hey,

thank you for reading this post.

Why won't this work?

outer = []
func = ->(n) { outer << n }
func[1]

=> outer stays empty. Why that? Since outer is readable in the lambda
function it should be changeable as it is the same instance of the
array. I am a bit confused.

Thanks you very much for your answers.
ms

IAmNan

unread,
Apr 20, 2012, 8:32:08 PM4/20/12
to rubyonra...@googlegroups.com
Are you doing this in the model, and is outer a member variable? If so, did you forget to use self on it?

self.outer = []
func = lambda { |n| self.outer << n }
func[1]

IAmNan

unread,
Apr 20, 2012, 8:35:51 PM4/20/12
to rubyonra...@googlegroups.com
Oh, here's with the new notation you're using. Sorry about that.

self.outer = []
func = -> n { self.outer << n }
func[1]

ms

unread,
Apr 21, 2012, 7:17:29 AM4/21/12
to Ruby on Rails: Talk
Thank you very much for your answer. I did not mention that I am not
using Rails in this case.

But the problem disappeared this morning - somehow. :)
Reply all
Reply to author
Forward
0 new messages