# Re: can there be a "with" construction?

**URL:** <https://www.ruby-forum.com/t/re-can-there-be-a-with-construction/73888>\
**Category:** Ruby\
**Created:** [October 27, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/re-can-there-be-a-with-construction/73888 "2006-10-27T20:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [October 27, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/re-can-there-be-a-with-construction/73888/1 "2006-10-27T20:00:10Z")

</div>

matt neuburg wrote:

> can even chain these tests:
> 
> with system.temp, system.startup {  
> string(license)  
> }
> 
> That means we try system.temp.license and if that doesn’t exist we  
> proceed to system.startup.license.
> 
> So my question is: is Ruby amenable to this kind of construction? Is  
> there a way to bend the language to that it acts like this? Thx - m.

Something like this?

def with(\*objects)  
begin  
yield  
rescue NoMethodError =\> exception  
o = objects.detect { |\_o|  
\_o.respond\_to? exception.name  
} or raise

```
o.send exception.name, *exception.args

```

end  
end

m = with(Time) do  
“aaa”.now  
end

p m  
Fri Oct 27 10:57:44 PDT 2006

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [October 27, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/re-can-there-be-a-with-construction/73888/2 "2006-10-27T20:10:12Z")

</div>

On 10/27/06, Gennady B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > UserTalk, you  
> > there a way to bend the language to that it acts like this? Thx - m.
> 
> ```
> o.send exception.name, *exception.args
> 
> ```
> 
> end  
> end

It’s a start, but:

1. I don’t think we really want to redispatch NoMethodErrors for  
messages that had an explicit receiver. Only the ones with implicit  
receivers.

2. it stops execution of the block after redispatching the first  
exception:

with(Time) do  
“aaa”.now  
puts “Never get here”  
end # nothing printed

Jacob F.
