Skip to content

Instantly share code, notes, and snippets.

@maliabadi
Created October 26, 2012 07:38
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 maliabadi/3957449 to your computer and use it in GitHub Desktop.
Save maliabadi/3957449 to your computer and use it in GitHub Desktop.
File One
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