[ create a new paste ] login | about

Link: http://codepad.org/tv2X1Eck    [ raw code | output | fork ]

sah - Ruby, pasted on Jan 26:
class NoOp
  def self.elif(cond)
    NoOp
  end
  def self.else
    NoOp
  end
end

class Ifer
  def initialize(cond)
    @cond = cond
  end

  def then
    if @cond
      yield
      NoOp
    else
      self
    end
  end

  def elif(cond)
    if cond
      yield
      NoOp
    else
      self
    end
  end

  def else
    if !@cond
      yield
      NoOp
    else
      self
    end
  end
end

def _if(cond)
  Ifer.new cond
end

x = 4
_if(x > 3).then{ p "big" }.elif(x > 0){ p "medium"}.else{ p "small" }
x = 1
_if(x > 3).then{ p "big" }.elif(x > 0){ p "medium"}.else{ p "small" }
x = 0
_if(x > 3).then{ p "big" }.elif(x > 0){ p "medium"}.else{ p "small" }


Output:
1
2
3
"big"
"medium"
"small"


Create a new paste based on this one


Comments: