Created
November 5, 2011 01:28
-
-
Save mungruby/1340943 to your computer and use it in GitHub Desktop.
A display_superheroes method that accepts an alternative block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Superhero = Struct.new :name, :origin, :nemesis, :nick_name | |
SuperHeroes = [ | |
Superhero.new("Batman", "Gotham City", "Joker", "Caped Crusader"), | |
Superhero.new("Robin", "Gotham City", "Joker", "Boy Wonder"), | |
Superhero.new("Superman", "Krypton", "Lex Luthor", "Kal El"), | |
Superhero.new("Supergirl", "Krypton", "Bizzaro", "Kara Zor-El") ] | |
def display_superheroes *superheroes, &block | |
superheroes.flatten.each do |hero| | |
if block_given? | |
yield hero | |
else | |
puts | |
puts hero.inspect | |
puts | |
end | |
end | |
end | |
display_superheroes SuperHeroes | |
display_superheroes SuperHeroes do |superhero| | |
puts %Q{ | |
Name: #{superhero.name}, | |
Origin: #{superhero.origin}, | |
Nemesis: #{superhero.nemesis}, | |
Nickname: #{superhero.nick_name} | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment