Skip to content
Implementing if in Ruby without using keywords.
Find file
New pull request
Latest commit 89efb22 @mudge Add license
Failed to load latest commit information.
lib Support Ruby 1.8.7
spec Support Ruby 1.8.7
.rspec Add chainable if_true and if_false methods
Gemfile Add Rake to Gemfile for Travis CI
LICENSE Add license Add license
if.gemspec Add license

if Build Status

See "Implementing if in Ruby" for more information.


This library is tested on Ruby 1.8.7 and later but benefits from Ruby 1.9's hash and lambda literal syntax.

"truthy".if -> { "I'm true!" }, else: -> { "I'm false!" }
# vs.
"truthy".if proc { "I'm true!" }, :else => proc { "I'm false!" }


require "if"

"Some truthy object".if -> { "I'm true!" }, else: -> { "I'm false!" }
#=> "I'm true!"

nil.if -> { "I'm true!" }, else: -> { "I'm false!" }
#=> "I'm false!"

# Or, if you only care about side-effects and not return value:

"Some truthy object"
  .if_true { puts "I'm true!" }
  .if_false { puts "I'm false!" }
# "I'm true!"
#=> "Some truthy object"

  .if_true { puts "I'm true!" }
  .if_false { puts "I'm false!" }
# "I'm false!"
#=> nil


Copyright © 2015 Paul Mucur

Distributed under the MIT License.

Something went wrong with that request. Please try again.