Reputation: 5174
If I have a user model.
sample_user = User.all.first
Is there a way in Ruby to get type back in a String? Something like:
typeof(sample_user) == "User"
Upvotes: 5
Views: 3927
Reputation: 6682
If you are doing comparison, use #kind_of?
or #is_a?
sample_user = User.first
sample_user.kind_of? User
#=> true
sample_user.is_a? User
#=> true
Upvotes: 3
Reputation: 7366
You can do it multiple ways :
1st :
sample_user.is_a? User
2nd :
sample_user.class.to_s == "User"
and more :). But if you want to check class type with object. Then 1st One would be good.
Upvotes: 6