#returning and #tap

On Fri, 17 Nov 2006, Eric H. wrote:

$ wc

6 14 77

Ten characters and one line more typing, but less punctuation. (And those
ten extra characters will be mostly be handled by my tab key.)

and quite easy to golf if line count matters that much

def foo
@foo ||= Foo.new.instance_eval{ self.bar = ‘bar’; self }
end

-a

Eric H. wrote:

readability of the code and uselessly increased the complexity.

Otherwise you have to do this:
$ wc

6 14 77

Ten characters and one line more typing, but less punctuation. (And
those ten extra characters will be mostly be handled by my tab key.)

But you have to type @foo five times, instead of once. Oh, well. It’s
a matter of taste.

On 11/16/06, [email protected] [email protected] wrote:

and quite easy to golf if line count matters that much

def foo
@foo ||= Foo.new.instance_eval{ self.bar = ‘bar’; self }
end

def foo; @foo ||= Foo.new.instance_eval{ self.bar = ‘bar’; self } end

smirks

[email protected] wrote:

@foo

def foo
@foo ||= Foo.new.instance_eval{ self.bar = ‘bar’; self }
end

-a

ROFL

Coming full circle around the golf course… This last one above is what
lead me to prefer #then (== #tap) in the first place:

def foo
@foo ||= Foo.new.then do |f|
f.bar = “bar”
end
end

Also, #then avoids the scoping nastiness of #instance_eval.

[email protected] wrote:

end
end

Also, #then avoids the scoping nastiness of #instance_eval.

I’m not getting the semantics of “then”. Do you mean “then” as in, “I
took off my coat and then sat down”, as opposed to then in if/then?

Yes. Not much better than #tap, is it? Any ideas…? “and_then”?

Hi –

On Fri, 17 Nov 2006, Joel VanderWerf wrote:

Coming full circle around the golf course… This last one above is what lead
me to prefer #then (== #tap) in the first place:

def foo
@foo ||= Foo.new.then do |f|
f.bar = “bar”
end
end

Also, #then avoids the scoping nastiness of #instance_eval.

I’m not getting the semantics of “then”. Do you mean “then” as in, “I
took off my coat and then sat down”, as opposed to then in if/then?

David

Hi –

On Fri, 17 Nov 2006, Joel VanderWerf wrote:

f.bar = “bar”
end
end

Also, #then avoids the scoping nastiness of #instance_eval.

I’m not getting the semantics of “then”. Do you mean “then” as in, “I
took off my coat and then sat down”, as opposed to then in if/then?

Yes. Not much better than #tap, is it? Any ideas…? “and_then”?

“whereupon” :slight_smile: I guess it’s hard to find a name that’s general
enough (as opposed to, say, “post_initialize”) but also expressive
enough. It does seem a slightly odd technique to me, in any case –
sort of like saying “Now I’m going to do this: this” instead of just
going to a new line and saying: “this”. I’m not sure how much of the
oddness I feel is the technique as opposed to the naming issue.

David

[email protected] wrote:

what lead me to prefer #then (== #tap) in the first place:
took off my coat and then sat down", as opposed to then in if/then?

Yes. Not much better than #tap, is it? Any ideas…? “and_then”?

“whereupon” :slight_smile: I guess it’s hard to find a name that’s general
enough (as opposed to, say, “post_initialize”) but also expressive
enough. It does seem a slightly odd technique to me, in any case –
sort of like saying “Now I’m going to do this: this” instead of just
going to a new line and saying: “this”. I’m not sure how much of the
oddness I feel is the technique as opposed to the naming issue.

Does the following technique seem odd?

class Foo
attr_accessor :a, :b, :c
def initialize
yield self if block_given?
end
end

foo = Foo.new do |f|
f.a = 1
f.b = 2
f.c = 3
end

#then is just a way of using arbitrary classes (or factories) in this
way. Maybe the name should not emphasize the temporal (“then” or
“whereupon”); so maybe “tap” is better, or “configured_using”.

Btw, I don’t think I’ve ever used #then in more than 5 or 6 places.
Usually, I just try to define classes like Foo above.

On Fri, 17 Nov 2006, Joel VanderWerf wrote:

f.bar = “bar”
end
end

Also, #then avoids the scoping nastiness of #instance_eval.

I’m not getting the semantics of “then”. Do you mean “then” as in, “I
took off my coat and then sat down”, as opposed to then in if/then?

Yes. Not much better than #tap, is it? Any ideas…? “and_then”?

class Object
def as obj
end

-a

On Fri, 17 Nov 2006, Joel VanderWerf wrote:

ROFL

Coming full circle around the golf course… This last one above is what lead
me to prefer #then (== #tap) in the first place:

heh.

def foo
@foo ||= Foo.new.then do |f|
f.bar = “bar”
end
end

Also, #then avoids the scoping nastiness of #instance_eval.
^^^^^^^^^^^^^^^^^

indeed - good point.

-a

[email protected] wrote:

def foo
Yes. Not much better than #tap, is it? Any ideas…? “and_then”?

class Object
def as obj
end

To me, “as do” doesn’t read as well as “then do”.

On Nov 16, 2006, at 1:57 PM, Joel VanderWerf wrote:

   6      14      77

Ten characters and one line more typing, but less punctuation.
(And those ten extra characters will be mostly be handled by my
tab key.)

But you have to type @foo five times, instead of once. Oh, well.
It’s a matter of taste.

No, just @. I like my friendly editors.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

On 11/17/06, [email protected] [email protected] wrote:

Part of what’s odd to me about #then is that it’s not
constructor-specific. Once it’s defined you could do:

x = string.split(‘,’).then do |array|
array.map! { something }
end

“then” actually works fine for me there, Englishwise. Maybe and_then
would read slightly better:

string.split(‘,’).and_then do |array|

martin

Hi –

On Fri, 17 Nov 2006, Martin DeMello wrote:

would read slightly better:

string.split(’,’).and_then do |array|

I wonder if there’s some name that embodies the fact that the object
is going to come back from this method call. The temporal ones (then,
and_then, etc.) sound kind of self-evident (“Well, of course the next
thing happens ‘then’,” my brain says). Perhaps that was the reasoning
behind “tap”, though “tap” doesn’t communicate much to me. I’m not
sure.

David

Hi –

On Fri, 17 Nov 2006, Joel VanderWerf wrote:

f.a = 1
f.b = 2
f.c = 3
end

#then is just a way of using arbitrary classes (or factories) in this way.
Maybe the name should not emphasize the temporal (“then” or “whereupon”); so
maybe “tap” is better, or “configured_using”.

Part of what’s odd to me about #then is that it’s not
constructor-specific. Once it’s defined you could do:

x = string.split(’,’).then do |array|
array.map! { something }
end

Bad example :slight_smile: But that’s what I meant about not giving it too
specific a name, like “post_initialize”, since it’s not restricted to
initialization scenarios.

David

On 11/17/06, [email protected] [email protected] wrote:

I wonder if there’s some name that embodies the fact that the object
is going to come back from this method call. The temporal ones (then,
and_then, etc.) sound kind of self-evident (“Well, of course the next
thing happens ‘then’,” my brain says).

Good point! I thought of “pipe” but even that doesn’t say that it
modifies and returns self. Maybe “apply”: string.split(/\s/).apply
{|i| i.pop}.whatever. Or even “modify”.

Perhaps that was the reasoning
behind “tap”, though “tap” doesn’t communicate much to me. I’m not
sure.

‘tap’ had a different intent, though - “tee off the object without
disturbing it” - even if it did the same thing in the end. So you
would typically take a.foo.bar.baz.quux… and drop in a tap,
a.foo.bar.tap {|i| puts “hi mom! this is #{i}”}.baz.quux, and take
care not to modify i destructively.

martin

Actually, how about giving the proc a copy of the object, rather than
the real deal?

class Object
def tap
yield self.dup
self
end
end

I definitely think of it as tapping a phone line.

a.map.map.map.map

eavesdrop = Proc.new { |secret| puts secret }

a.map.map.tap(&eavesdrop) .map.map

It lets me get into the line and hear what’s going on without changing
any existing behaviour. Using a proc that modifies secret is not a
good idea, just keep it for things with other types of side-effects.
There’s no way to mark the object read-only for the purposes of the
passed-in proc, is there?

I haven’t seen anything to change my mind about the other use. It just
seems pointless.

On 11/17/06, Martin DeMello [email protected] wrote:

Thought of that, but not everything responds to dup (nil and symbols, e.g.)

Can those cases be worked around?

On 11/17/06, spooq [email protected] wrote:

Actually, how about giving the proc a copy of the object, rather than
the real deal?

class Object
def tap
yield self.dup
self
end
end

Thought of that, but not everything responds to dup (nil and symbols,
e.g.)

m.