Skip to content

Instantly share code, notes, and snippets.

@jesseract
Created February 26, 2015 14:23
Show Gist options
  • Save jesseract/fa57e1c65ee587309a6a to your computer and use it in GitHub Desktop.
Save jesseract/fa57e1c65ee587309a6a to your computer and use it in GitHub Desktop.
I'm pretty sure the only thing I've retained in six weeks of code school is the fact that the little dinosaur you get when your internet dies in chrome is actually a playable game.
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.
# WRITE YOUR CODE HERE. In this challenge, names longer than 5 characters are
# considered long.
def has_even?(array)
array.any? {|i| i.even?}
end
def all_short?(array)
array.all? {|word| word.length <= 5}
end
def squares(array)
array.map(|n| n * n)
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