Skip to content

Instantly share code, notes, and snippets.

@TylerRockwell
Created September 23, 2015 13:24
Show Gist options
  • Select an option

  • Save TylerRockwell/3ff0cfeeb1a98c2f6dc6 to your computer and use it in GitHub Desktop.

Select an option

Save TylerRockwell/3ff0cfeeb1a98c2f6dc6 to your computer and use it in GitHub Desktop.
Enumerable Challenge
require 'minitest/autorun'
require 'minitest/pride'
# Write a series of methods which use the .any, .all, .map, .select, .reject, and
# .reduce methods in Enumerable. Each of your methods should be one line.
def has_even?(arr)
arr.any?(&:even?)
end
def all_short?(arr)
arr.all?{|name| name.length < 6}
end
def squares(arr)
arr.map{|x| x**2}
end
def just_short(arr)
arr.select{|name| name.length < 6}
end
def no_long(arr)
arr.reject{|name| name.length > 5}
end
def product(arr)
arr.reduce{|product, x| product * x}
end
class EnumerableChallenge < MiniTest::Test
def test_any
assert has_even?([2, 3, 4, 5, 6])
assert has_even?([-2, 3, -4, 5, -6])
refute has_even?([3, 5])
refute has_even?([-3, -5])
end
def test_all
assert all_short?(["Amy", "Bob", "Cam"])
assert all_short?(["Zeke", "Yoo", "Xod"])
refute all_short?(["Amy", "Bob", "Cammie"])
refute all_short?(["Zachary", "Yoo", "Xod"])
end
def test_map
assert_equal [1, 4, 9], squares([1, 2, 3])
assert_equal [16, 36, 81], squares([4, 6, 9])
end
def test_select
assert_equal ["Amy", "Bob", "Cam"], just_short(["Amy", "Bob", "Cam"])
assert_equal ["Zeke", "Yoo", "Xod"], just_short(["Zeke", "Yoo", "Xod"])
assert_equal ["Amy", "Bob"], just_short(["Amy", "Bob", "Cammie"])
assert_equal ["Yoo", "Xod"], just_short(["Zachary", "Yoo", "Xod"])
end
def test_reject
assert_equal ["Amy", "Bob", "Cam"], no_long(["Amy", "Bob", "Cam"])
assert_equal ["Zeke", "Yoo", "Xod"], no_long(["Zeke", "Yoo", "Xod"])
assert_equal ["Amy", "Bob"], no_long(["Amy", "Bob", "Cammie"])
assert_equal ["Yoo", "Xod"], no_long(["Zachary", "Yoo", "Xod"])
end
def test_reduce
assert_equal 1, product([1, 1, 1])
assert_equal 150, product([3, 5, 10])
assert_equal 0, product([18, 13, 0])
assert_equal 12, product([2, 3, 2])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment