Your own if/else

  • Thread starter Marcin Raczkowski
  • Start date
M

Marcin Raczkowski

Hello i remeber someone asking fif it's possible in Ruby - and i have no
idea if it was replyied, anyway i wanted to share my idea how to make
your own if/else/elsif :)

class Object
def else
self
end

def elsif(condition,&block)
self
end

def if(condition,&block)
(!condition && Else) || (condition && block.call)
end
end

class Else
def self.else(&block)
block.call
end

def self.elsif(condition, &block)
self.if(condition, &block)
end
end



and now we can do:

irb(main):024:0> self.if(true){:true}.else{:false}
=> :true
irb(main):025:0> self.if(false){:true}.else{:false}
=> :false
irb(main):051:0> self.if(false){:true}.elsif(true){:true}.else{nil}
=> :true
irb(main):052:0> self.if(false){:true}.elsif(false){:true}.else{nil}
=> nil
irb(main):053:0> self.if(true){:eek:k}.elsif(true){:not_ok}.else{:not_ok}
=> :eek:k
irb(main):056:0> self.if(false){:not_ok}.elsif(true){:eek:k}.else{:not_ok}
=> :eek:k


this aproach is elegant becouse even if object have method else or elif
or even if declared then it'll bahave as expected !!!
Also i didn't use any of old if/else statements, i didn't even use tri
argument operator (cond ? true : false), it's all implemented using
boolean logic!!

Ok, now probably comes question - what we can do with it - of course -
create less boolean logic!!!

module Fuzzy
def self.if(float, &block)
condition = rand <= float
(!condition && Else) || (condition && block.call)
end
end

now we provide if function with float between 0 and 1 - which represents
probability of achieveing goal

Fuzzy.if(0.99){puts "pretty standard stuff"}.else{puts "**** man, you
really have luck"}

with some luck (3th try for me) you can get something like this:
irb(main):097:0> 10.times{ Fuzzy.if(0.99){puts "pretty standard
stuff"}.else{puts "**** man, you really have luck"} }
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
**** man, you really have luck
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
=> 10

What else - of course this way you can do random mutations in genetic
algorithms, of course there's other ways to achieve that - but this one
is fun :D

currently there's only one problem with this:

irb(main):099:0> self.if(false){:not_ok}
=> Else
while normally you get nil, also Else evaluates to >true< in
expressions, i thought about using Nil or False class instead of else -
but then block returning nil or false would trigger else block too.
so there's tradeoff. Also Else class could cause name conficts so
someone can rename it to
WierdClassThatServesOnlyAsMetaProgramingMagickElse to avoid it :)


------------

And last thing - i tried hacking ruby interpreter to treat >if< not as
keyword - but as Kernel method - but not much luck yet,


have phun with Ruby :)
 
T

Trans

Hello i remeber someone asking fif it's possible in Ruby - and i have no
idea if it was replyied, anyway i wanted to share my idea how to make
your own if/else/elsif :)

class Object
def else
self
end

def elsif(condition,&block)
self
end

def if(condition,&block)
(!condition && Else) || (condition && block.call)
end
end

class Else
def self.else(&block)
block.call
end

def self.elsif(condition, &block)
self.if(condition, &block)
end
end

and now we can do:

irb(main):024:0> self.if(true){:true}.else{:false}
=> :true
irb(main):025:0> self.if(false){:true}.else{:false}
=> :false
irb(main):051:0> self.if(false){:true}.elsif(true){:true}.else{nil}
=> :true
irb(main):052:0> self.if(false){:true}.elsif(false){:true}.else{nil}
=> nil
irb(main):053:0> self.if(true){:eek:k}.elsif(true){:not_ok}.else{:not_ok}
=> :eek:k
irb(main):056:0> self.if(false){:not_ok}.elsif(true){:eek:k}.else{:not_ok}
=> :eek:k

this aproach is elegant becouse even if object have method else or elif
or even if declared then it'll bahave as expected !!!
Also i didn't use any of old if/else statements, i didn't even use tri
argument operator (cond ? true : false), it's all implemented using
boolean logic!!

Ok, now probably comes question - what we can do with it - of course -
create less boolean logic!!!

module Fuzzy
def self.if(float, &block)
condition = rand <= float
(!condition && Else) || (condition && block.call)
end
end

now we provide if function with float between 0 and 1 - which represents
probability of achieveing goal

Fuzzy.if(0.99){puts "pretty standard stuff"}.else{puts "**** man, you
really have luck"}

with some luck (3th try for me) you can get something like this:
irb(main):097:0> 10.times{ Fuzzy.if(0.99){puts "pretty standard
stuff"}.else{puts "**** man, you really have luck"} }
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
**** man, you really have luck
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
pretty standard stuff
=> 10

What else - of course this way you can do random mutations in genetic
algorithms, of course there's other ways to achieve that - but this one
is fun :D

currently there's only one problem with this:

irb(main):099:0> self.if(false){:not_ok}
=> Else

I bet you could fix that with lazy evaluation.

T.
 
C

Curt Sampson

Actually, I find the Smalltalk way a little more elegant. The booleans
have true:, false:, and true:false: methods; each of the parameters
takes a block:

x > 3 true: [ 1 + 1 ] false [ 0 ]

(If I recall correctly.)

cjs
 
M

Marcin Raczkowski

Curt said:
Actually, I find the Smalltalk way a little more elegant. The booleans
have true:, false:, and true:false: methods; each of the parameters
takes a block:

x > 3 true: [ 1 + 1 ] false [ 0 ]

(If I recall correctly.)

cjs
Well you can do it with ruby (and even c)like this:

x >3 ? [1+2] : [0]

:)
Inyaway i just wanted to make someone's life little easier, and/or more
fun :)
 
R

Rick DeNatale

Actually, I find the Smalltalk way a little more elegant. The booleans
have true:, false:, and true:false: methods; each of the parameters
takes a block:

x > 3 true: [ 1 + 1 ] false [ 0 ]

(If I recall correctly.)

x > 3 ifTrue:[1 + 1] ifFalse:[0]

However, most Smalltalk implementations cheat and the compiler turns
this into test and branch bytecodes rather than sending the
ifTrue:ifFalse: message
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top