https://github.com/mudge/if
Implementing if in Ruby without using keywords.
https://github.com/mudge/if
Last synced: 8 months ago
JSON representation
Implementing if in Ruby without using keywords.
- Host: GitHub
- URL: https://github.com/mudge/if
- Owner: mudge
- License: mit
- Created: 2012-07-09T21:43:04.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2015-03-15T13:08:47.000Z (over 11 years ago)
- Last Synced: 2025-10-25T08:39:04.404Z (8 months ago)
- Language: Ruby
- Homepage: http://mudge.github.com/2012/07/09/implementing-if-in-ruby.html
- Size: 145 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# if [](http://travis-ci.org/mudge/if)
See "[Implementing `if` in Ruby](http://mudge.github.com/2012/07/09/implementing-if-in-ruby.html)" for more information.
## Requirements
This library is tested on Ruby 1.8.7 and later but benefits from Ruby 1.9's
hash and lambda literal syntax.
```ruby
"truthy".if -> { "I'm true!" }, else: -> { "I'm false!" }
# vs.
"truthy".if proc { "I'm true!" }, :else => proc { "I'm false!" }
```
## Usage
```ruby
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"
nil
.if_true { puts "I'm true!" }
.if_false { puts "I'm false!" }
# "I'm false!"
#=> nil
```
## License
Copyright © 2015 Paul Mucur
Distributed under the MIT License.