I have a user object which i access like this.. user.email, user.first_name
Now I want to loop through each and return a message based on the current key in loop somewhat like this.
[user.first_name, user.email, user.last_name].each do |key|
puts "#{key} is required"
end
The above works but loops through values
instead of fields
. so will do this below but how to prepend user to keys and get values inside array?
[first_name, email, last_name].each do |key|
#inside here I want to prepend the user to the keys so that I can access like user.key
puts "#{user.key} is required"
end
If this was a string we can concatenate but how to deal with this here as its a method call?