def convert_elements_to_symbols(elements)
symbols = []
elements.each{|el| symbols << el.to_sym}
symbols
end
def transform_to_percentage(ratios)
[].tap do |percentages|
ratios.each{|ratio| percentages << ratio * 100}
end
end
def convert_elements_to_symbols(elements)
symbols = []
elements.each{|el| symbols << el.to_sym}
symbols
end
class Array
def map
collection = []
each{|item| collection << yield(item)}
collection
end
end
def convert_elements_to_symbols(elements) elements.map(&:to_sym)
end
def extract_even_numbers(numbers)
even_numbers = []
numbers.each{|number| even_numbers << number if number.even?}
even_numbers
end
class Array
def select
items = []
each{|item| items << item if yield(item)}
items
end
end
def extract_even_numbers(numbers)
numbers.select(&:even?)
end
def find_user_by_email(users, email)
users.each do |user|
return user if user.email == email
end
nil
end
class Array
def detect
each{|item| return item if yield(item)}
nil
end
end
def find_user_by_email(users, email)
users.detect{|user| user.email == email}
end
%w(a b c).detect(->{'default'}) do |i|
i == 'd'
end #=> "default"
def names_without_a(names)
aless_names = []
names.each do |name|
aless_names << name unless name =~ /a/i
end
aless_names
end
class Array
def reject
select{|item| !yield(item)}
end
end
def names_without_a(names)
names.reject{|name| name =~ /a/i}
end
def total_adjustment_amount(adjustments)
total = 0
adjustments.each{|adjustment| total += adjustment}
total
end
class Array
def inject(start)
accumulator = start
each{|item| accumulator = yield(accumulator, item)}
accumulator
end
end
def total_adjustment_amount(adjustments)
adjustments.inject(0){|accu, adjustment| accu + adjustment}
end
def create_identity_map(users)
map = {}
users.each{|user| map[user.id] = user}
map
end
class Array
def each_with_object(object)
each{|item| yield(object, item)}
object
end
end
def create_identity_map(users)
users.each_with_object({}){|map, user| map[user.id] = user}
end
def everyone_over_18?(teens)
teens.each{|teen| return false unless teen.over_18?}
true
end
class Array
def all?
each{|item| return false unless yield(item)}
true
end
end
def everyone_over_18?(teens)
teens.all?(&:over_18?)
end
def anyone_over_18?(teens)
teens.each{|teen| return true if teen.over_18?}
false
end
class Array
def any?
each{|item| return true if yield(item)}
end
end
def anyone_over_18?(teens)
teens.any?(&:over_18?)
end
def valid_name?(name)
VALID_NAMES.each{|valid_name| return true if valid_name == name}
false
end
class Array
def include?(item)
any?{|member| member == item}
end
end
def valid_name?(name)
VALID_NAMES.include?(name)
end
def order_by_age(people)
return people if people.size <= 1 # already sorted swapped = true while swapped do swapped = false 0.upto(people.size-2) do |i| if people[i].age > people[i+1].age people[i], people[i+1] = people[i+1], people[i] # swap values swapped = true end end end people
end
class Array
def sort
mergesort
end
end
def order_by_age(people)
people.sort{|alice, bob| bob.age <=> alice.age}
end
class Array
def sort_by
sort{|a, b| yield(a) <=> yield(b)}
end
end
def order_by_age(people)
people.sort_by(&:age)
end
def combine_lists(lists)
full_list = []
lists.each do |list|
full_list.concat(list)
end
full_list
end
class Array
def flatten
each_with_object([]){|list, item| list.concat(item)}
end
end
def combine_lists(lists)
lists.flatten
end
def create_index(words)
index = Hash.new{[]}
words.each do |word|
index[word[0]] << word
end
index
end
class Array
def group_by
each_with_object(Hash.new{[]}){|obj, item|
obj[yield(item)] << item}
end
end
def create_index(words)
words.group_by{|word| word[0]}
end
def words_that_appear_3_times(words) word_counts = Hash.new{0} words.each do |word| word_counts[word] += 1 end
three_peats = [] word_counts.each do |word, count| three_peats << word if count == 3 end three_peats end words = %w(bob bob bob bill bill jim jim jim jim jake jake jake) words_that_appear_3_times(words)
def words_that_appear_3_times(words)
words.group_by(&:to_s).select{|_, occurrences| occurrences.size == 3}.map{|word, _| word}
end
words = %w(bob bob bob bill bill jim jim jim jim jake jake jake)
words_that_appear_3_times(words)