Can there be a "with" construction?

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.

On 10/27/06, matt neuburg [email protected] 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.
:wink:

Jacob F.

Jacob F. [email protected] wrote:

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.

On 10/27/06, matt neuburg [email protected] wrote:

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

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

Kalman

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 N. wrote:

Kalman

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

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

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

On 10/27/06, John W. [email protected] wrote:

On 10/27/06, matt neuburg [email protected] 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://johnwilger.com/articles/2006/10/27/adding-with-to-ruby


Regards,
John W.

Joel VanderWerf:

Kalman N. wrote:

class Object
def with
yield self
self
end
end

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

matt neuburg wrote:

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.

argument.
Yes, that’s too why I like »with«.

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”?

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

I don’t know much German, but as far as I know, “eigen” means something along
the lines of “characteristic of” or “particular to”.

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.

Kalman

Kalman N. wrote:

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”?

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

To be clear, ‘eigenclass’ was one (popular) suggestion among many for
renaming the object you get when you write:
foo = class << bar; self; end
that has historically most commonly been referred to as the ‘singleton
class’ of an object.

See http://wiki.rubygarden.org/Ruby/page/show/RenamingSingletonClass
for more names. :slight_smile:

Kalman N. 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?

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)

Is the behavior the original poster asks about analogous to the the
behavior of the with construct in vb.net which is as follows:

with cbocoffee
.items.add(.text)
.text=“”
end with

or am I missing something?

Kalman N. wrote:

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”?

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

I don’t know much German, but as far as I know, “eigen” means something along
the lines of “characteristic of” or “particular to”.

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