A few block-related questions

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.

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

yes,

2. how can I pass argument to block?

use #instance_exec (1.9 only)

Guy Decoux

You can as well pass self as argument:

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

a=A.new
blk.call a, str

Regards

robert

···

2006/5/8, Victor Shepelev <vshepelev@imho.com.ua>:

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?

--
Have a look: Robert K. | Flickr

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

yes,

> 2. how can I pass argument to block?

use #instance_exec (1.9 only)

Thanks a lot! I use exactly 1.9.

Guy Decoux

Victor.

···

From: ts [mailto:decoux@moulon.inra.fr]

> 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?

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.

···

From: Robert Klemme [mailto:shortcutter@googlemail.com]

2006/5/8, Victor Shepelev <vshepelev@imho.com.ua>: