Created
October 26, 2012 07:38
-
-
Save maliabadi/3957449 to your computer and use it in GitHub Desktop.
File One
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
class Person | |
attr_accessor :youth, :last_name, :first_name | |
def initialize(first, last, age) | |
@youth = age | |
@first_name = first | |
@last_name = last | |
end | |
end | |
an_array_of_hashes = [{'first' => 'jen', | |
'last' => 'ng', | |
'age' => 25 }, | |
{'first' => 'matt', | |
'last' => 'aliabadi', | |
'age' => 26 }, | |
{'first' => 'nicole', | |
'last' => 'likarish', | |
'age' => 25 }] | |
# a method that takes in an array of hashes, and returns an array of instances of the class 'Person' | |
def generate_people_from_array_of_hashes(arr) | |
return arr.map do |h| | |
Person.new(h['first'], h['last'], h['age']) | |
end | |
end | |
# declaration, and invocation (of the generate_people_from_array_of_hashes method) | |
people = generate_people_from_array_of_hashes(an_array_of_hashes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment