0.0
No commit activity in last 3 years
No release in over 3 years
Non-native conditionals in ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6
~> 10.0
~> 3.0
 Project Readme

Conditional

Ruby's if/elsif/else, but not using native constructs. Mostly just as an exercise on how ruby should have implemented ifs, but it has some practical uses (see usage).

Installation

Add this line to your application's Gemfile:

gem 'conditional'

And then execute:

$ bundle

Or install it yourself as:

$ gem install conditional

Usage

It works almost exactly like ruby's native constructs, except for:

  • Body of conditionals have their own scope
  • else/else_nil branch must be specified
  • The condition is passed to the block

The third point comes in very useful when dealing with regexes.

Instead of

match = %r{^http://github.com/(.*?)/}.match(url)

user_name = if match
  match[1]
else
  raise 'Incorrect url format'
end

which pollutes your current scope with the throw-away match variable, you can write

user_name = If %r{^http://github.com/(.*?)/}.match(url) do |match|
  match[1]
end.else do
  raise 'Incorrect url format'
end

elsifs are supported too:

If if_condition do
  # do something
end.elsif another_condition do
  # or this
end.else do
  # otherwise this
end

If you don't want to supply an if condition, you can use else_nil:

value_or_nil = If condition do
  :value
end.else_nil

Contributing

  1. Fork it ( https://github.com/cameron-martin/conditional/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request