Sha256: bf6983edae90908e8838c82684629013ecf457d094c0ab2883abe21b6d3bf4df

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

class Object
  def false? ; nil ; end
  def true? ; yield ; end
end

class FalseClass
  def false? ; yield ; end
  def true? ; nil ; end
end

class NilClass
  def false? ; yield ; end
  def true? ; nil ; end
end

module Kernel
  def _if(exp, &block)
    else_block = callcc { |cont|
      @condition = cont
      return exp.true?(&block)
    }
    @condition = nil
    exp.false?(&else_block)
  end
  def _unless(exp, &block)
    else_block = callcc { |cont|
      @condition = cont
      return exp.false?(&block)
    }
    @condition = nil
    exp.true?(&else_block)
  end
  def _else(&block)
    @condition.call(block) if @condition
  end
end


# --- Test ---
 
a = nil
b = 2
c = false

_if(a != b) {
  puts "a!=b"
}
_else {
  puts "a==b"
}

_unless(b) {
  puts "b is false"
}
_else {
  puts "b is true"
}


# I was thinking of allowing for a multi block scenario.
module Kernel
  alias fn lambda
  def iff( *exp_blks )
    r = nil
    exp_blks.each_with_index{ |e,i| 
      if i % 2 == 0
        r = e.true?(&exp_blks[i+1]); break if e
      end
    }
    r
  end
  def unles( *exp_blks )
    r = nil
    exp_blks.each_with_index{ |e,i| 
      if i % 2 == 0
        r = e.false?(&exp_blks[i+1]); break if ! e
      end
    }
    r
  end
end

# --- test ---

a = nil
b = 2
c = false

iff(
  (a != b), fn{
    puts "a!=b"
  }, 
  (true), fn{
    puts "a==b"
  } 
)

unles( 
  (b), fn{
    puts "b is false"
  }, 
  (false), fn{
    puts "b is true"
  }
)

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
carats-0.3.0 lib/carat-dev/conditional/conditionals.rb
facets-1.4.1 forge/more/conditionals.rb
facets-1.4.2 forge/more/conditionals.rb
facets-1.4.3 forge/more/conditionals.rb
facets-1.4.5 snip/more/conditionals.rb
facets-1.4.4 forge/more/conditionals.rb