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
endclass Arraydef mapcollection = []each{|item| collection << yield(item)}collectionendenddef 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_numbersend
class Arraydef selectitems = []each{|item| items << item if yield(item)}itemsendenddef extract_even_numbers(numbers)numbers.select(&:even?)end
def find_user_by_email(users, email)users.each do |user|return user if user.email == emailendnilend
class Arraydef detecteach{|item| return item if yield(item)}nilendenddef 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/iendaless_namesend
class Arraydef rejectselect{|item| !yield(item)}endenddef names_without_a(names)names.reject{|name| name =~ /a/i}end
def total_adjustment_amount(adjustments)total = 0adjustments.each{|adjustment| total += adjustment}totalend
class Arraydef inject(start)accumulator = starteach{|item| accumulator = yield(accumulator, item)}accumulatorendenddef 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}mapend
class Arraydef each_with_object(object)each{|item| yield(object, item)}objectendenddef 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?}trueend
class Arraydef all?each{|item| return false unless yield(item)}trueendenddef everyone_over_18?(teens)teens.all?(&:over_18?)end
def anyone_over_18?(teens)teens.each{|teen| return true if teen.over_18?}falseend
class Arraydef any?each{|item| return true if yield(item)}endenddef anyone_over_18?(teens)teens.any?(&:over_18?)end
def valid_name?(name)VALID_NAMES.each{|valid_name| return true if valid_name == name}falseend
class Arraydef include?(item)any?{|member| member == item}endenddef 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 peopleend
class Arraydef sortmergesortendenddef order_by_age(people)people.sort{|alice, bob| bob.age <=> alice.age}end
class Arraydef sort_bysort{|a, b| yield(a) <=> yield(b)}endenddef order_by_age(people)people.sort_by(&:age)end
def combine_lists(lists)full_list = []lists.each do |list|full_list.concat(list)endfull_listend
class Arraydef flatteneach_with_object([]){|list, item| list.concat(item)}endenddef combine_lists(lists)lists.flattenend
def create_index(words)index = Hash.new{[]}words.each do |word|index[word[0]] << wordendindexend
class Arraydef group_byeach_with_object(Hash.new{[]}){|obj, item|obj[yield(item)] << item}endenddef 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 endthree_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)