Idiom wanted (now hiring!)

G

greg

assuming a is already defined

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

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

Joel VanderWerf

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

CHubas

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
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top