Skip to content

Instantly share code, notes, and snippets.

@ncaron

ncaron/enum.rb Secret

Created January 19, 2018 15:31
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 ncaron/759e132a0f0e3c16e8b18797a13763d3 to your computer and use it in GitHub Desktop.
Save ncaron/759e132a0f0e3c16e8b18797a13763d3 to your computer and use it in GitHub Desktop.
module Enumerable
def my_each
for element in self
yield element
end
self
end
def my_each_with_index
for i in (0...self.length)
yield(self[i], i)
end
self
end
def my_select
selected_elements = []
self.my_each { |element| selected_elements.push(element) if yield(element) }
selected_elements
end
def my_all? *args
if block_given?
self.my_each { |element| return false if !yield element }
elsif args[0]
self.my_each { |element| return false if !args[0].match?(element) }
else
self.my_each { |element| return false if !element }
end
true
end
def my_any? *args
if block_given?
self.my_each { |element| return true if yield element }
elsif args[0]
self.my_each { |element| return true if args[0].match?(element) }
else
self.my_each { |element| return true if element }
end
false
end
def my_none? *args
if block_given?
self.my_each { |element| return false if yield element }
elsif args[0]
self.my_each { |element| return false if args[0].match?(element) }
else
self.my_each { |element| return false if element }
end
true
end
def my_count *args
count = 0
if block_given?
self.my_each { |element| count += 1 if yield element }
elsif args[0]
self.my_each { |element| count += 1 if element == args[0] }
else
self.my_each { |element| count += 1 }
end
count
end
def my_map *args
mapped = []
self.my_each { |element| mapped.push(yield element) }
mapped
end
def my_inject *args
if args[0].is_a? Symbol
block = args[0].to_proc
total = 0
elsif args[1].is_a? Symbol
block = args[1].to_proc
total = args[0]
else
total = args[0] || 0
end
if block_given?
self.my_each { |element| total = yield(total, element) }
else
self.my_each { |element| total = block.call(total, element) }
end
total
end
end
puts "MY_EACH"
each_test = [1, 2, 3].each { |element| element }
my_each_test = [1, 2, 3].my_each { |element| element }
puts each_test == my_each_test
puts "MY_EACH_WITH_INDEX"
each_with_index_test = ["Hello", "I'm", "Niko"].each_with_index { |element, index| "#{index}: #{element}" }
my_each_with_index_test = ["Hello", "I'm", "Niko"].my_each_with_index { |element, index| "#{index}: #{element}" }
puts each_with_index_test == my_each_with_index_test
puts "MY_SELECT"
select_test = [1, 2, 3, 4, 5].select { |element| element > 3 }
my_select_test = [1, 2, 3, 4, 5].my_select { |element| element > 3 }
puts select_test == my_select_test
puts "MY_ALL?"
all_test = [5, 60, 81, 16].all? { |element| element >= 5 }
my_all_test = [5, 60, 81, 16].my_all? { |element| element >= 5 }
puts all_test = my_all_test
all_test = ["ant", "bear", "cat"].all?(/t/)
my_all_test = ["ant", "bear", "cat"].my_all?(/t/)
puts all_test == my_all_test
all_test = [nil, true, 99].all?
my_all_test = [nil, true, 99].my_all?
puts all_test == my_all_test
all_test = [].all?
my_all_test = [].my_all?
puts all_test == my_all_test
puts "MY_ANY?"
any_test = [5, 60, 81, 16].any? { |element| }
my_any_test = [5, 60, 81, 16].my_any? { |element| }
puts any_test == my_any_test
any_test = ["ant", "bear", "cat"].any?(/d/)
my_any_test = ["ant", "bear", "cat"].my_any?(/d/)
puts any_test == my_any_test
any_test = [nil, true, 99].any?
my_any_test = [nil, true, 99].my_any?
puts any_test == my_any_test
any_test = [].any?
my_any_test = [].my_any?
puts any_test == my_any_test
puts "MY_NONE?"
none_test = [5, 60, 81, 16].none? { |element| }
my_none_test = [5, 60, 81, 16].my_none? { |element| }
puts none_test == my_none_test
none_test = ["ant", "bear", "car"].none?(/d/)
my_none_test = ["ant", "bear", "car"].my_none?(/d/)
puts none_test == my_none_test
none_test = [].none?
my_none_test = [].my_none?
puts none_test == my_none_test
none_test = [nil, false].none?
my_none_test = [nil, false].my_none?
puts none_test == my_none_test
puts "MY_COUNT"
count_test = [1, 2, 3, 4].count
my_count_test = [1, 2, 3, 4].my_count
puts count_test == my_count_test
count_test = [1, 2, 3, 4].count(2)
my_count_test = [1, 2, 3, 4].my_count(2)
puts count_test == my_count_test
count_test = [1, 2, 3, 4].count { |x| x % 2 == 0 }
my_count_test = [1, 2, 3, 4].my_count { |x| x % 2 == 0 }
puts count_test == my_count_test
puts "MY_MAP"
map_test = (1..4).map { |i| i * i }
my_map_test = (1..4).my_map { |i| i * i }
puts map_test == my_map_test
square_proc = Proc.new { |i| i**2 }
map_test = (1..4).map &square_proc
my_map_test = (1..4).my_map &square_proc
puts map_test == my_map_test
puts "MY_INJECT"
inject_test = (5..10).inject { |sum, number| sum + number }
my_inject_test = (5..10).my_inject { |sum, number| sum + number }
puts inject_test == my_inject_test
inject_test = (5..10).inject(1) { |product, n| product * n }
my_inject_test = (5..10).my_inject(1) { |product, n| product * n }
puts inject_test == my_inject_test
inject_test = (5..10).inject(1, :*)
my_inject_test = (5..10).my_inject(1, :*)
puts inject_test == my_inject_test
inject_test = (5..10).inject(:+)
my_inject_test = (5..10).my_inject(:+)
puts inject_test == my_inject_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment