[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how do i turn an object into a binding?

Bret Pettichord

1/28/2005 10:18:00 PM

I have code such as this:

foo.bar
foo.baz(1)
foo.bagaloo('didoo')

instead i want to write it like this:

with (foo) do
bar
baz(1)
bagaloo('didoo')
end

how?


_____________________
Bret Pettichord
www.pettichord.com



3 Answers

Florian Frank

1/28/2005 10:30:00 PM

0

Bret Pettichord wrote:

> how?


foo.instance_eval do
bar
baz(1)
bagaloo('didoo')
end

--
Florian Frank



Austin Ziegler

1/29/2005 12:34:00 AM

0

On Sat, 29 Jan 2005 07:29:59 +0900, Florian Frank <flori@nixe.ping.de> wrote:
> Bret Pettichord wrote:
>
> > how?
>
> foo.instance_eval do
> bar
> baz(1)
> bagaloo('didoo')
> end

Note that this won't work if you try to use assignment, because that
will be interpreted as a local variable. Better:

module Kernel
def with(obj)
yield self if block_given?
end
end

with longfooname do |f|
f.bar
f.baz(1)
f.bagaloo('didoo')
end

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Florian Frank

1/29/2005 2:00:00 PM

0

Austin Ziegler wrote:

>Note that this won't work if you try to use assignment, because that
>will be interpreted as a local variable.
>
True. In this case 'self' could be used:

foo.instance_eval do
self.quux = true
end

If assignment methods haven't yet been defined in a class definition,
the same problem occurs.

>Better:
>
> module Kernel
> def with(obj)
> yield self if block_given?
> end
> end
>
> with longfooname do |f|
> f.bar
> f.baz(1)
> f.bagaloo('didoo')
> end
>
>-austin
>
>
Well, than you could do f = longfooname at the beginning as well. ;)

--
Florian Frank