I have a User model with a number of very similar properties that I want to list without typing each on in individually.
So, rather than:
"eye color: #{@user.his_eye_color}"
"hair color: #{@user.his_hair_color}"
"height: #{@user.his_height}"
"weight: #{@user.his_weight}"
...
"eye color: #{@user.her_eye_color}"
"hair color: #{@user.her_hair_color}"
"height: #{@user.her_height}"
"weight: #{@user.her_weight}"
...
I'd like to do a block or something (Proc? Lambda? Still a touch unclear what those are...):
['eye color','hair color','height','weight',...].do |stat|
"#{stat}: #{@user.her_(stat.underscore)}"
end
['eye color','hair color','height','weight',...].do |stat|
"#{stat}: #{@user.his_(stat.underscore)}"
end
I know that what I just wrote above is mystical, magical, and wholly and completely WRONG (the @user.his_(stat.underscore)
part), but what could I do that's like this? I basically need to call my Model's attributes dynamically, but I'm unsure how to do this...
Any help would be really appreciated!