Idiom wanted (now hiring!)

On 4/14/07, Christian N. [email protected] wrote:

“Robert D.” [email protected] writes:

You see it is not the isolated semantics of x=x, but the context of
other semantics that disturbs me (well disturbs me just a tiny little
bit ;).

So you are proposing all undefined locals return nil too? }}:slight_smile:
No I am too young to die, because that means we need python call syntax
for the implicit receiver, maybe you want some parens too ;).
Beware I am writing this in the best intent.
Ok fooled around enough.

The real problem is the implicit declaration of variables of course.
I feel that it should be hidden which means it should take place after
the RHS evaluation, but in practice it takes place before.

Well I can life with that :slight_smile:

Cheers
Robert

“Robert D.” [email protected] writes:

Beware I am writing this in the best intent.
Ok fooled around enough.

The real problem is the implicit declaration of variables of course.
I feel that it should be hidden which means it should take place after
the RHS evaluation, but in practice it takes place before.

Well I can life with that :slight_smile:

In my five years of using Ruby, this has bitten me maybe twice.

I think I misspelled “initialize” a lot more. :stuck_out_tongue:

On 4/15/07, Christian N. [email protected] wrote:

I think I misspelled “initialize” a lot more. :stuck_out_tongue:
That for sure, that was a great PITN at first!!

Cheers
Robert

Christian N. [email protected] http://chneukirchen.org

Robert

On 4/14/07, Christian N. [email protected] wrote:

a does exist at the assignment and initialized as nil.

It’s very philosophical:

$ ruby -e ‘thing = thing; p thing’
nil

That might be to my liking too if there were not

======================
501/1 > ruby -e ‘@x=@x;p @x
nil
--------------------------->
503/3 > ruby -e ‘p @x
nil

504/4 > ruby -e ‘x=x;p x’
nil
-------------------------->
505/5 > ruby -e ‘p x’
-e:1: undefined local variable or method `x’ for main:Object (NameError)

You see it is not the isolated semantics of x=x, but the context of
other semantics that disturbs me (well disturbs me just a tiny little
bit ;).

Cheers
Robert

assuming a is already defined

class Object; def yield; yield(self) end end

func(b).yield{|fb| a = fb if fb}

On Apr 14, 10:49 am, Christian N. [email protected]
wrote:

“Robert D.” [email protected] writes:

You see it is not the isolated semantics of x=x, but the context of
other semantics that disturbs me (well disturbs me just a tiny little
bit ;).

So you are proposing all undefined locals return nil too? }}:slight_smile:


Christian N. [email protected] http://chneukirchen.org

Perhaps Kernel.variable_missing?
Or I’m just crazy…

On 4/15/07, CHubas [email protected] wrote:

Christian N. [email protected] http://chneukirchen.org

Perhaps Kernel.variable_missing?
Or I’m just crazy…
I hope not ;), but I think this will enter in conflict with
method_missing.

Robert

On Apr 15, 3:28 pm, “Robert D.” [email protected] wrote:

So you are proposing all undefined locals return nil too? }}:slight_smile:

Perhaps Kernel.variable_missing?
Or I’m just crazy…

I hope not ;), but I think this will enter in conflict with method_missing.

Robert

I think it’s not a preposterous (translation dictionaries are silly)
idea after all, if handled well. Say, method_missing would have more
precedence than variable_missing.

As for now, method missing is worth a try

def method_missing
nil
end

foo
=> nil
x ||= 10
=> 10
x
=> 10

but this is obviously wicked. Specially with typos and lexical
mistakes. I think it’s worse to get bad results from bad valued than
get an error and make things right instead.

Oh, and it doesn’t handle constants.

FOO
=> NameError: uninitialized constant OK
from (irb):32
from :0

On Apr 13, 1:02 am, Jonathan [email protected] wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

greg wrote:

assuming a is already defined

class Object; def yield; yield(self) end end

func(b).yield{|fb| a = fb if fb}

Shades of forth:

class Object; def if?; yield(self) if self end end

a = 1
“fred wilma”[/fred/].if?{|x| a = x}
p a # ==> “fred”
“fred wilma”[/barney/].if?{|x| a = x}
p a # ==> “fred”

Btw, I think this construct has appeared before, but I can’t seem to
recall what it was called…