oo00oo
2006-Mar-04 18:40 UTC
[Rails] Determine if a value is an instance of String , or Array , or...
each loop inside a mixed hash I need to determine if the value is an instance of String How ? hash.each do |k,v| if v ( instance of ) String hash[ k ] = v.capitalize enf end
Rick Tessner
2006-Mar-04 18:56 UTC
[Rails] Determine if a value is an instance of String , or Array , or...
On 3/4/06, oo00oo <oo00oo@free.fr> wrote:> > each loop inside a mixed hash > I need to determine if the value is an instance of String > How ? > > hash.each do |k,v| > if v ( instance of ) String > hash[ k ] = v.capitalize > enf > endHere''s two different ways of doing it ... since I''m still pretty new to ruby, I''m not sure if either of them is the best way (both use capitalize! rather to capitalize to change the value "in-place"): hash.each do |k,v| v.capitalize! rescue v end OR hash.each do |k,v| v.capitalize! if v.respond_to? :capitalize end Rick -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060304/5ae1fe66/attachment.html
oo00oo
2006-Mar-04 19:07 UTC
[Rails] Determine if a value is an instance of String , or Array , or...
Thanks for the tips inline rescue is nice ! But I''m always searching a method to determine a type. For Arrays, Hashs...> > On 3/4/06, *oo00oo* <oo00oo@free.fr <mailto:oo00oo@free.fr>> wrote: > > each loop inside a mixed hash > I need to determine if the value is an instance of String > How ? > > hash.each do |k,v| > if v ( instance of ) String > hash[ k ] = v.capitalize > enf > end > > > Here''s two different ways of doing it ... since I''m still pretty new > to ruby, I''m not sure if either of them is the best way (both use > capitalize! rather to capitalize to change the value "in-place"): > > hash.each do |k,v| > v.capitalize! rescue v > end > > OR > > hash.each do |k,v| > v.capitalize! if v.respond_to? :capitalize > end > > Rick >
Tore Darell
2006-Mar-04 19:10 UTC
[Rails] Re: Determine if a value is an instance of String , or Array
hash.each{|k,v| v.capitalize! if v.is_a? String } Or use Rick''s "duck typing" approach. oo00oo wrote:> each loop inside a mixed hash > I need to determine if the value is an instance of String > How ? > > hash.each do |k,v| > if v ( instance of ) String > hash[ k ] = v.capitalize > enf > end-- Posted via http://www.ruby-forum.com/.
Tore Darell
2006-Mar-04 19:16 UTC
[Rails] Re: Determine if a value is an instance of String , or Array
object.is_a?(ClassName) does this. object.class returns the class. oo00oo wrote:> Thanks for the tips > inline rescue is nice ! > But I''m always searching a method to determine a type. For Arrays, > Hashs...-- Posted via http://www.ruby-forum.com/.
Pete Yandell
2006-Mar-04 21:37 UTC
[Rails] Determine if a value is an instance of String , or Array , or...
Also worth mentioning that you can do this: hash.each do |k, v| case v when String v.capitalize when Array # do something else else # do something else again end end Pete Yandell http://9cays.com On 05/03/2006, at 6:07 AM, oo00oo wrote:> Thanks for the tips > inline rescue is nice ! > But I''m always searching a method to determine a type. For Arrays, > Hashs... >> >> On 3/4/06, *oo00oo* <oo00oo@free.fr <mailto:oo00oo@free.fr>> wrote: >> >> each loop inside a mixed hash >> I need to determine if the value is an instance of String >> How ? >> >> hash.each do |k,v| >> if v ( instance of ) String >> hash[ k ] = v.capitalize >> enf >> end >> >> >> Here''s two different ways of doing it ... since I''m still pretty >> new to ruby, I''m not sure if either of them is the best way (both >> use capitalize! rather to capitalize to change the value "in-place"): >> >> hash.each do |k,v| >> v.capitalize! rescue v >> end >> >> OR >> >> hash.each do |k,v| >> v.capitalize! if v.respond_to? :capitalize >> end >> >> Rick >> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Bob Silva
2006-Mar-05 00:25 UTC
[Rails] Determine if a value is an instance of String , or Array , or...
hash = {''a'' => ''bob'', ''b'' => 3, ''c'' => ''fred'' } hash.each_value {|d| d.capitalize! if d.is_a?(String) } => {"a"=>"Bob", "b"=>3, "c"=>"Fred"} So, to answer your question, use the is_a? (or kind_of?) method. Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of oo00oo > Sent: Saturday, March 04, 2006 10:41 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] Determine if a value is an instance of String , or Array > ,or... > > each loop inside a mixed hash > I need to determine if the value is an instance of String > How ? > > hash.each do |k,v| > if v ( instance of ) String > hash[ k ] = v.capitalize > enf > end > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails