Skip to content

Instantly share code, notes, and snippets.

@mpereira
Created April 24, 2011 02:04
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 mpereira/939237 to your computer and use it in GitHub Desktop.
Save mpereira/939237 to your computer and use it in GitHub Desktop.
battle_cries = [{ king_leonidas: 'This is where we fight! This is where they die!' },
{ he_man: 'By the power of Greyskull... I have the powerrr!' },
{ sindel: 'You are pathetic and weak.' },
{ shang_tsung: 'Your soul is mine!' },
{ klingons: 'Today is a good day to die!' }]
def from_mortal_kombat?(battle_cry)
[:sindel, :shang_tsung].include?(battle_cry.keys.first)
end
puts 'The old, ugly way. Well, at least for me.'
mortal_kombat_battle_cries = []
battle_cries.each_with_index do |battle_cry, index|
if from_mortal_kombat?(battle_cry)
mortal_kombat_battle_cries << [battle_cry, index]
end
end
p mortal_kombat_battle_cries
puts 'New shiny way.'
mortal_kombat_battle_cries = battle_cries.each.with_index.map do |battle_cry, index|
from_mortal_kombat?(battle_cry) ? [battle_cry, index] : nil
end.compact
p mortal_kombat_battle_cries
puts 'Or even like this if you like to inject.'
mortal_kombat_battle_cries = battle_cries.each.with_index.inject([]) do |memo, (battle_cry, index)|
from_mortal_kombat?(battle_cry) ? memo << [battle_cry, index] : memo
end
p mortal_kombat_battle_cries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment