public
Last active

  • Download Gist
array_if.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.