[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

can there be a "with" construction?

matt

10/27/2006 5:31:00 PM

Some languages have a "with" construction, where undefined methods are
routed to a designated object. Here's an example from UserTalk:

with system.startup {
string(license)
}

UserTalk knows what "string" is, but when it can't find "license" it
reinterprets it as system.startup.license, which works. In UserTalk, you
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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...
16 Answers

Paul Lutus

10/27/2006 5:50:00 PM

0

matt neuburg wrote:

> Some languages have a "with" construction, where undefined methods are
> routed to a designated object. Here's an example from UserTalk:
>
> with system.startup {
> string(license)
> }
>
> UserTalk knows what "string" is, but when it can't find "license" it
> reinterprets it as system.startup.license, which works. In UserTalk, you
> 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.

Not AFAIK. IMHO the ultimate "with" construction is to create a class or
module that includes all the required named methods and then operate inside
the class. This gives you the same shorthand syntax and seems to me to be
more attuned to OO principles.

--
Paul Lutus
http://www.ara...

Jacob Fugal

10/27/2006 5:52:00 PM

0

On 10/27/06, matt neuburg <matt@tidbits.com> wrote:
> Some languages have a "with" construction, where undefined methods are
> routed to a designated object. Here's an example from UserTalk:
>
> with system.startup {
> string(license)
> }
>
> UserTalk knows what "string" is, but when it can't find "license" it
> reinterprets it as system.startup.license, which works.

Barebones approach:

system.startup.instance_eval do
...
end

instance_eval sets "self" for the block so all messages with an
implicit receiver go to it's receiver.

> In UserTalk, you can even chain these tests:
>
> with system.temp, system.startup {
> string(license)
> }

This is a little harder, in that you can't do it with one already
existing method, but it is possible. A way in which I can think of
doing it is to create a proxy instance that can contain multiple
target instances. The first target instance (they're ordered by
priority) that responds to the message will process it. A "with"
method that automatically creates this proxy around it's arguments
then instance_evals that block on the proxy would do what you're
looking for, I think.

The code to actually do the above is left as an exercise for the reader. ;)

Jacob Fugal

matt

10/27/2006 6:17:00 PM

0

Jacob Fugal <lukfugl@gmail.com> wrote:

> On 10/27/06, matt neuburg <matt@tidbits.com> wrote:
> > Some languages have a "with" construction, where undefined methods are
> > routed to a designated object. Here's an example from UserTalk:
> >
> > with system.startup {
> > string(license)
> > }
> >
> > UserTalk knows what "string" is, but when it can't find "license" it
> > reinterprets it as system.startup.license, which works.
>
> Barebones approach:
>
> system.startup.instance_eval do
> ...
> end
>
> instance_eval sets "self" for the block so all messages with an
> implicit receiver go to it's receiver.

Perfect-o-rama! So I can use "with" syntax by defining this:

def with(ref, &block)
ref.instance_eval &block
end

Thanks, that's great. m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

John Wilger

10/27/2006 6:44:00 PM

0

On 10/27/06, matt neuburg <matt@tidbits.com> wrote:
> Some languages have a "with" construction, where undefined methods are
> routed to a designated object. Here's an example from UserTalk:
>
> with system.startup {
> string(license)
> }
>
> UserTalk knows what "string" is, but when it can't find "license" it
> reinterprets it as system.startup.license, which works. In UserTalk, you
> can even chain these tests:
>
> with system.temp, system.startup {
> string(license)
> }
>

Off the top of my head, you could do:

$with_receivers = []

def with( *receivers )
prev_receivers = $with_receivers
$with_receivers = receivers
begin
yield if block_given?
ensure
$with_receivers = prev_receivers
end
end

def method_missing( meth, *args )
receiver = $with_receivers.detect do |r|
r.respond_to? meth
end
if receiver
return receiver.send( meth, *args )
else
super
end
end

There's definitely some nastiness there with the global variable, and
I'm sure someone is going to point out where this completely falls
apart within minutes of me posting it -- but it works for simple
cases, at least.

--
Regards,
John Wilger
http://john...

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland

Kalman Noel

10/27/2006 7:15:00 PM

0

matt neuburg:
> Some languages have a "with" construction, where undefined methods are
> routed to a designated object. Here's an example from UserTalk:
>
> with system.startup {
> string(license)
> }

I sometimes feel as if something like this might be a nice Ruby idiom:

class Object
def with
yield self
self
end
end

@object = Something.new.with { |x|
x.name = 'Chunky bacon'
x.description = 'A very tasty piece of finest chunky bacon.'
x.size = 12
}

Kalman

Kalman Noel

10/27/2006 7:38:00 PM

0

I wrote:
> class Object
> def with
> yield self
> self
> end
> end

Just noticed that similar things are being discussed here:
http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object...

Kalman

Joel VanderWerf

10/27/2006 7:51:00 PM

0

Kalman Noel wrote:
> I wrote:
>> class Object
>> def with
>> yield self
>> self
>> end
>> end
>
> Just noticed that similar things are being discussed here:
> http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object...
>
> Kalman

See also the discussion of #then yesterday on ruby-talk. It's defined
exactly as the #with method above.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Paul Brannan

10/27/2006 8:34:00 PM

0

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

See [ruby-talk:41867] and the following thread.

Paul

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

dave rose

10/27/2006 8:43:00 PM

0

shouldn't this .with method be better evaluated with the method_missing?
construct ?

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

John Wilger

10/27/2006 10:07:00 PM

0

On 10/27/06, John Wilger <johnwilger@gmail.com> wrote:
> On 10/27/06, matt neuburg <matt@tidbits.com> wrote:
> > Some languages have a "with" construction, where undefined methods are
> > routed to a designated object. Here's an example from UserTalk:
> >
> > with system.startup {
> > string(license)
> > }
...
> Off the top of my head, you could do:
...
> There's definitely some nastiness there with the global variable, and
> I'm sure someone is going to point out where this completely falls
> apart within minutes of me posting it -- but it works for simple
> cases, at least.

OK, it was bothering me enough that I went ahead and fleshed out a
better solution (complete with tests!) --
http://john.../articles/2006/10/27/adding-wi...

--
Regards,
John Wilger
http://john...

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland