the instrument, and it may be formulated as follows: Give a small boy a hammer, and he will find that everything he encounters needs pounding.” - Abraham Kaplan
the Ruby toolbox #each is the most overused Ruby method I think the documentation structure is partly to blame: Enumerable gets mixed in and is therefore documented separately
- throughout Ruby docs Enumerators are chainable. You can create your own Enumerators. You can do some really weird stuff with Enumerators. Let’s get to know our instrument
|r, v| group, key, value = v # array decomposition r[group] ||= {} # make sure group exists r[group][key] = value # set key/value in group r # return value for next end
|value| # destructure to name values if hash[value[0]] # hash default hash[value[0]][value[1]] = value[2] # hash default else hash[value[0]] = {value[1] => value[2]} # ONLY I MATTER end end
}.select { |n| n % 7 == 0 } arr.each_with_object([]) do |n, arr| x = n*n arr << x if x % 7 == 0 end # user system total real # map select 1.970000 0.050000 2.020000 ( 2.045519) # each_with_object 1.430000 0.000000 1.430000 ( 1.439536)
all the Fish.'.split capitalized_count = 0 words.each { |w| capitalized_count += 1 if w =~ /^[A-Z]/ } capitalized_count words.count { |w| w =~ /^[A-Z]/ }
prime number using a **custom** Ruby method" # is_prime?(27,13,42) => {27=>false, 13=>true, 42=>false} def is_prime?(*nums) i = 2 nums.each do |num| while i < num is_divisible = ((num % i) == 0) if is_divisible == false x = "#{num}: is NOT a prime number." #false else x = "#{num}: is a prime number." #true end i +=1 end return x end end