Skip to content

Instantly share code, notes, and snippets.

@mhaley
Created January 20, 2010 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhaley/281687 to your computer and use it in GitHub Desktop.
Save mhaley/281687 to your computer and use it in GitHub Desktop.
# Source: http://gist.github.com/281687#file_array_if.rb
class Array
def if(predicate)
predicate ? self.first : self.last
end
end
if __FILE__ == $0
require 'test/unit'
class ArrayTest < Test::Unit::TestCase
def test_should_return_expensive
price = 100
assert_equal "expensive", ["expensive","cheap"].if(price > 50)
end
def test_should_return_cheap
price = 25
assert_equal "cheap", ["expensive","cheap"].if(price > 50)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment