Erwin
2011-Apr-18 22:09 UTC
script or utility to transform a string w embedded spaces into a sym
I need to transform strings like "Instructor ID" or "Lovely Ice tree" into sym :instructor_id , :lovely_ice_tree I tried to do the following : downcase all string components the unique the spaces and replace w an underscore is there a better and faster way to do it ? thanks fy feedback -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Apr-18 22:38 UTC
Re: script or utility to transform a string w embedded spaces into a sym
On Apr 18, 2011, at 3:09 PM, Erwin wrote:> I need to transform strings like "Instructor ID" or "Lovely Ice tree" > into sym :instructor_id , :lovely_ice_tree > I tried to do the following : > downcase all string components the unique the spaces and replace w an > underscore > > is there a better and faster way to do it ?string.downcase.tr('' '', ''_'') maybe. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Apr-19 05:53 UTC
Re: script or utility to transform a string w embedded spaces into a sym
On 18 April 2011 23:09, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I need to transform strings like "Instructor ID" or "Lovely Ice tree" > into sym :instructor_id , :lovely_ice_tree > > is there a better and faster way to do it ?I have a little monkey-patch called "dehumanize" which is *almost* what you want: module ActiveSupport::Inflector # does the opposite of humanize.... mostly. Basically does a # space-substituting .underscore def dehumanize(string_value) result = string_value.to_s.dup result.downcase.gsub(/ +/,''_'') end end class String def dehumanize ActiveSupport::Inflector.dehumanize(self) end end you could then call: String.dehumanize.to_sym -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Erwin
2011-Apr-19 14:44 UTC
Re: script or utility to transform a string w embedded spaces into a sym
Thanks , I''ll use this patch .. On 19 avr, 07:53, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 April 2011 23:09, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > I need to transform strings like "Instructor ID" or "Lovely Ice tree" > > into sym :instructor_id , :lovely_ice_tree > > > is there a better and faster way to do it ? > > I have a little monkey-patch called "dehumanize" which is *almost* > what you want: > > module ActiveSupport::Inflector > # does the opposite of humanize.... mostly. Basically does a > # space-substituting .underscore > def dehumanize(string_value) > result = string_value.to_s.dup > result.downcase.gsub(/ +/,''_'') > end > end > class String > def dehumanize > ActiveSupport::Inflector.dehumanize(self) > end > end > > you could then call: > String.dehumanize.to_sym-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Erwin
2011-Apr-19 14:45 UTC
Re: script or utility to transform a string w embedded spaces into a sym
thanks , part of the solution as per Michael''s patch On 19 avr, 00:38, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> On Apr 18, 2011, at 3:09 PM, Erwin wrote: > > > I need to transform strings like "Instructor ID" or "Lovely Ice tree" > > into sym :instructor_id , :lovely_ice_tree > > I tried to do the following : > > downcase all string components the unique the spaces and replace w an > > underscore > > > is there a better and faster way to do it ? > > string.downcase.tr('' '', ''_'') > > maybe.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.