can there be a "with" construction?

M

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)
}

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

Jacob Fugal

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
 
M

matt neuburg

Jacob Fugal said:
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.
 
J

John Wilger

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://johnwilger.com

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

Kalman Noel

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
 
P

Paul Brannan

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
 
D

Dave Rose

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

John Wilger

...
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://johnwilger.com/articles/2006/10/27/adding-with-to-ruby

--
Regards,
John Wilger
http://johnwilger.com

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

Kalman Noel

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

It seems that this pattern has been quite popular for some time already.
It may be desirable for the list to try and agree on a great name for the
method. Anyone who implements it calls it differently: Object#then,
Object#with, Object#tap, Kernel#returning, Kernel#with, ... Anyone with
arguments for or against certain names, or with a better name for the
method?

It seems to me that it's impossible to invent a useful name for this
method with the term »eigen« in it, so a discussion should be possible
without too much battle.

Kalman
 
P

poopdeville

Kalman Noel wrote:

It seems that this pattern has been quite popular for some time already.
It may be desirable for the list to try and agree on a great name for the
method. Anyone who implements it calls it differently: Object#then,
Object#with, Object#tap, Kernel#returning, Kernel#with, ... Anyone with
arguments for or against certain names, or with a better name for the
method?


<snip>

I suppose I'll cast my vote for either "with" or "suchthat" or even ":"
(though IIRC, ":" can't be a method name). They are all idiomatic in
mathematical circles when introducing a new mathematical object in an
argument. To be fair, the colon is shorthand for "such that". Though
there are some stylistic/usage differences between "with" and "such
that", they are synonymous.

Examples:
Let V be a normed vector space. Let B be an orthogonal basis such that
each element b in B has length 1.

Let V be a vector space with orthonormal basis B.

Most (all?) of my programming work mirrors formal arguments and
designs, which is the primary reason I chose to work with an OO
language. That is, I introduce objects, operate on them, reason about
them, and terminate. Anything to bridge (increasingly smaller)
conceptual gap is helpful. I feel a lot more productive if my code
reads like the examples above. (This is also why I worked with Perl
before -- a lot of Perl reads like slightly formal, idiomatic English
to me.)
It seems to me that it's impossible to invent a useful name for this
method with the term »eigen« in it, so a discussion should be possible
without too much battle.

Interesting. Why "eigen"? I don't know much German, but as far as I
know, "eigen" means something along the lines of "characteristic of" or
"particular to". I can see the relation, but it somehow seems
linguistically backwards, since the code block is particular to the new
object created, instead of the object created being particular to the
code block.

'cid 'ooh

(Sorry if this is a repost. Google is broken)
 
P

poopdeville

Kalman Noel wrote:
(NB: I wrote »impossible«, not »possible«.) I intended to hint at the term
»eigenclass«, which is an example of a Ruby concept with more than one name.
It naming sometimes enjoys heavy discussions.

Whoops, sorry. I read your sentence as saying it was *impossible* to
name the method *without* the term "eigen" in it. My mistake.
It does. The inventors of the term »eigenclass« have supposingly thought of the
mathematical words »eigenvector« and »eigenvalue«. (You get them when solving
the characteristic equation of a matrix, assuming I got the English terms
right.) »Characteristic« and »eigen« mean the same here.

Yes, you got the terms right.

'cid 'ooh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,216
Messages
2,571,120
Members
47,720
Latest member
mohdkaif002

Latest Threads

Top