jmfreitas
2009-Jan-16 17:21 UTC
"obj.is_one_of? {Class1, Class2, ...}" as similar to "obj.is_a?(Class1) or obj.is_a?(Class2)"
I need to check if a given object is an instance of a range of Classes, and I was wondering if I could do it by passing in is_a? an hash of classes, instead of calling is_a? for each class I have to compare... As I state in the subject (obj.is_one_of? {Class1, Class2, ...}), this could justify a method "obj.is_one_of?" which returns true if the object "obj" is an instance of Class1 or Class2... Is there something that I''m missing? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2009-Jan-16 17:39 UTC
Re: "obj.is_one_of? {Class1, Class2, ...}" as similar to "obj.is_a?(Class1) or obj.is_a?(Class2)"
On Jan 16, 2009, at 12:21 PM, jmfreitas wrote:> I need to check if a given object is an instance of a range of > Classes,Are you sure? It would be more idiomatic to check for duck-typing: obj.respond_to? :my_method or to just call it and deal with the consequences: begin obj.my_method rescue NoMethodError # deal with it end You already have something similar with: case obj when Class1, Class2 # Yippee! else # Drat! end> and I was wondering if I could do it by passing in is_a? an > hash of classes, instead of calling is_a? for each class I have to > compare... > As I state in the subject (obj.is_one_of? {Class1, Class2, ...}), this > could justify a method "obj.is_one_of?" which returns true if the > object "obj" is an instance of Class1 or Class2... > Is there something that I''m missing?What are you trying to do that causes you to want this? -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jmfreitas
2009-Jan-16 18:35 UTC
Re: "obj.is_one_of? {Class1, Class2, ...}" as similar to "obj.is_a?(Class1) or obj.is_a?(Class2)"
Thanks so much for the quick and useful reply... The suggestion to use "case obj when Class1, Class2" completely fulfills my needs, most appreciated :-D On 16 Jan, 17:39, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jan 16, 2009, at 12:21 PM, jmfreitas wrote: > > > I need to check if a given object is an instance of a range of > > Classes, > > Are you sure? It would be more idiomatic to check for duck-typing: > obj.respond_to? :my_method > or to just call it and deal with the consequences: > begin > obj.my_method > rescue NoMethodError > # deal with it > end > > You already have something similar with: > case obj > when Class1, Class2 > # Yippee! > else > # Drat! > end > > > and I was wondering if I could do it by passing in is_a? an > > hash of classes, instead of calling is_a? for each class I have to > > compare... > > As I state in the subject (obj.is_one_of? {Class1, Class2, ...}), this > > could justify a method "obj.is_one_of?" which returns true if the > > object "obj" is an instance of Class1 or Class2... > > Is there something that I''m missing? > > What are you trying to do that causes you to want this? > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---