יום שבת, 11 במאי 2013

Ruby case when


a = 4

case a

when 1..2
puts "It's between 1 and 2"
when 1..5
puts "It's between 1 and 5"
when 6
puts "It's 6"
when String
puts "You passed a string"
else
puts "You gave me #{a} -- I have no idea what to do with that."
end
prints "It's between 1 and 5"
need to override the ===
for an example :
class MyCls2
def initialize (pParam)
  @myValue = pParam
end
 
def GetMyValue
  @myValue
end
 
def === (other_object)
  if  other_object.GetMyValue == @myValue
   true
  else
   false
  end
end
end 
a = MyCls2.new ( 2 )
b = MyCls2.new ( 2 )
case a
when b
  puts " - 1- "
else
  puts "You gave me #{a} -- I have no idea what to do with that."
end
 
 


אין תגובות:

הוסף רשומת תגובה