views:

76

answers:

3

Let's say I have two objects: User and Race.

class User
  attr_accessor :first_name
  attr_accessor :last_name
end

class Race
  attr_accessor :course
  attr_accessor :start_time
  attr_accessor :end_time
end

Now let's say I create an array of hashes like this:

user_races = races.map{ |race| {:user => race.user, :race => race} }

How do I then convert user_races into an array of structs, keeping in mind that I want to be able to access the attributes of both user and race from the struct element? (The key thing is I want to create a new object via Struct so that I can access the combined attributes of User and Race. For example, UserRace.name, UserRace.start_time.)

+1  A: 

Create a definition for the UserRace object (as a Struct), then just make an array of said objects.

UserRace = Struct.new(:user, :race)

user_races = races.map { |race| UserRace.new(race.user, race) }

# ...

user_races.each do |user_race|
  puts user_race.user
  puts user_race.race
end
Mark Rushakoff
The question seems a little vague. But this seems right to me.
Levi
Yea, apologies if vague. The key thing is I want to create a new object via Struct so that I can access the combined attributes of User and Race. For example, UserRace.name, UserRace.start_time. Make sense?
keruilin
+1 - This is an elegant solution, but i think it doesn't answer the real question i.e it doesn't actually CONVERT array of hashes to array of structs... though it will solve this particular problem.
Webbisshh
+1  A: 

Try this:

class User
  attr_accessor :first_name
  attr_accessor :last_name
end

class Race
  attr_accessor :course
  attr_accessor :start_time
  attr_accessor :end_time
end

UserRace = Struct.new(:first_name, :last_name, :course, :start_time, :end_time)

def get_user_race_info      
  user_races = races.map do |r| 
    UserRace.new(r.user.first_name, r.user.last_name, 
              r.course, r.start_time, r.end_time)
  end
end

Now let's test the result:

user_races = get_user_race_info
user_races[0].first_name
user_races[0].end_time
KandadaBoggu
If I try to put this as a method in the User class, like def self.fastest_time_record_holders races = Race.by_fastest_time UserRace = Struct.new(:first_name, :last_name, :course, :start_time, :end_time) user_races = races.map do |r| UserRace.new(r.user.first_name, r.user.last_name, r.course, r.start_time, r.end_time) end endI get a dynamic constant error. What do I need to do?
keruilin
You can't redefine the struct in your method. Put the struct definition out side the method body. I have updated my answer, please take a look
KandadaBoggu
A: 

You have this :::

user_races = races.map{ |race| {:user => race.user, :race => race} }

Now create a Struct as shown below :

UserRace = Struct.new(:user, :race)

And then ::

user_races.each do |user_race|
   new_array << UserRace.new(user_race[:user],user_race[:race])
end

Haven't tested the code... should be fine... what say?

EDIT: Here I am adding the objects of UserRace to a new_array.

Webbisshh