# With statement

**URL:** <https://www.ruby-forum.com/t/with-statement/124990>\
**Category:** Ruby\
**Created:** [January 4, 2008, 11:51am UTC](https://www.ruby-forum.com/t/with-statement/124990 "2008-01-04T11:51:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 11:51am UTC](https://www.ruby-forum.com/t/with-statement/124990/1 "2008-01-04T11:51:09Z")

</div>

I’m pretty sure ruby doesn’t have a with statement. Is it possible to  
emulate one?

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/with-statement/124990/2 "2008-01-04T13:05:50Z")

</div>

On Jan 4, 11:51 am, ole \_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/with-statement/124990/3 "2008-01-04T13:23:06Z")

</div>

Lars wrote:

> On Jan 4, 11:51 am, ole \_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/with-statement/124990/4 "2008-01-04T13:49:12Z")

</div>

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-forum.com/](http://www.ruby-forum.com/).

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

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/with-statement/124990/5 "2008-01-04T15:54:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [January 4, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/with-statement/124990/6 "2008-01-04T17:12:06Z")

</div>

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

On 1/4/08, ole \_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)
