Forum: Ruby A few block-related questions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-05-08 10:26
(Received via mailing list)
Hi all.

Here is the code:

str = 'some string'
blk = lambda{|arg| self.text = str; self.text = arg}

class A
    def text=(str)
       #blah
    end
end

A.new.instance_eval( &blk )

Questions about the code:
1. str would be always visible to block? (Quick tests says yes, but it
was
unexpected for me)
2. how can I pass argument to block?

Thanks.

Victor.
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-05-08 11:50
(Received via mailing list)
>>>>> "V" == Victor Shepelev <vshepelev@imho.com.ua> writes:

V> 1. str would be always visible to block? (Quick tests says yes, but
it was
V> unexpected for me)

 yes,

V> 2. how can I pass argument to block?

 use #instance_exec (1.9 only)


Guy Decoux
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-08 12:02
(Received via mailing list)
2006/5/8, Victor Shepelev <vshepelev@imho.com.ua>:
>     end
> end
>
> A.new.instance_eval( &blk )
>
> Questions about the code:
> 1. str would be always visible to block? (Quick tests says yes, but it was
> unexpected for me)
> 2. how can I pass argument to block?

You can as well pass self as argument:

blk = lambda {|it, text| it.text=text}

a=A.new
blk.call a, str

Regards

robert
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-05-08 15:48
(Received via mailing list)
From: ts [mailto:decoux@moulon.inra.fr]
>  use #instance_exec (1.9 only)
Thanks a lot! I use exactly 1.9.

> Guy Decoux

Victor.
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-05-08 15:51
(Received via mailing list)
From: Robert Klemme [mailto:shortcutter@googlemail.com]
> >        #blah
>
> You can as well pass self as argument:
>
> blk = lambda {|it, text| it.text=text}
>
> a=A.new
> blk.call a, str
>

Yes, I can. But it looks a bit ugly, isn't it?


> robert

Victor.
This topic is locked and can not be replied to.