[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

With statement

Oliver Saunders

1/4/2008 10:51:00 AM

I'm pretty sure ruby doesn't have a with statement. Is it possible to
emulate one?
--
Posted via http://www.ruby-....

5 Answers

Lars Christensen

1/4/2008 12:01:00 PM

0

On Jan 4, 11:51 am, ole __ <oliver.saund...@gmail.com> wrote:
> I'm pretty sure ruby doesn't have a with statement. Is it possible to
> emulate one?

Yes:

module Kernel
def with(obj, &b)
obj.instance_eval(&b)
end
end

Usage:

class Foo
def dostuff
puts "doing stuff in Foo"
end
end

foo = Foo.new

with foo do
dostuff
end

Rgds,
Lars

Tiziano Merzi

1/4/2008 12:23:00 PM

0

Lars wrote:
> On Jan 4, 11:51 am, ole __ <oliver.saund...@gmail.com> wrote:
>> I'm pretty sure ruby doesn't have a with statement. Is it possible to
>> emulate one?
>
> Yes:
>

Or

class Object
def with(&b)
instance_eval(&b)
end
end

#Usage:

class Foo
def dostuff
puts "doing stuff in Foo"
end
end

foo = Foo.new

foo.with do
dostuff
end

Tiziano

--
Posted via http://www.ruby-....

Morton Goldberg

1/4/2008 12:48:00 PM

0

On Jan 4, 2008, at 5:51 AM, ole __ wrote:

> I'm pretty sure ruby doesn't have a with statement. Is it possible to
> emulate one?
> --
> Posted via http://www.ruby-....

Maybe. You're not very clear on what semantics you expect from
'with'. But you might try

alias with instance_eval

Then you can write stuff such as:

obj.with do
foo
bar
end

in place of

obj.foo
obj.bar

Regards, Morton

Oliver Saunders

1/4/2008 2:54:00 PM

0

Great solutions thanks guys.
I think I'm going to go with the...

with obj do
method_from_obj
end

...one because it reads better as English.
--
Posted via http://www.ruby-....

Giles Bowkett

1/4/2008 4:11:00 PM

0

If you get my gem utility_belt, it has a with statement by Dan Yoder
in it. This thread came up about a month ago here. Weird to think that
this is a FAQ.

On 1/4/08, ole __ <oliver.saunders@gmail.com> wrote:
> Great solutions thanks guys.
> I think I'm going to go with the...
>
> with obj do
> method_from_obj
> end
>
> ...one because it reads better as English.
> --
> Posted via http://www.ruby-....
>
>


--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....