Richard
2007-Feb-20 06:24 UTC
Create a hyphen-separated set of letters derived from a string - How to?
Hi, This is such a trivial programming issue, but I can''t find a way to transform, say ''abc'' to ''a-b-c'' without using pattern matching. Any ideas? Thanks in Advance, Richard --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Feb-20 08:36 UTC
Re: Create a hyphen-separated set of letters derived from a string - How to?
not tested, but should work: lenght = word.size 1.upto(lenght) do |i| newword << word.slice(i,1) newword << "-" unless i == lenght end On 20 Feb., 07:24, "Richard" <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote:> Hi, > > This is such a trivial programming issue, but I can''t find a way to > transform, say ''abc'' to ''a-b-c'' without using pattern matching. > > Any ideas? > > Thanks in Advance, > Richard--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ian Leitch
2007-Feb-20 09:10 UTC
Re: Create a hyphen-separated set of letters derived from a string - How to?
irb(main):130:0> a = "abcdefg" => "abcdefg" irb(main):131:0> a.size.times do |i| irb(main):132:1* a.insert i*2, ''-'' irb(main):133:1> end => 7 irb(main):134:0> a = a[1..-1] => "a-b-c-d-e-f-g" irb(main):135:0> On 20/02/07, Thorsten <duplexxx-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > not tested, but should work: > > lenght = word.size > 1.upto(lenght) do |i| > newword << word.slice(i,1) > newword << "-" unless i == lenght > end > > On 20 Feb., 07:24, "Richard" > <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote: > > Hi, > > > > This is such a trivial programming issue, but I can''t find a way to > > transform, say ''abc'' to ''a-b-c'' without using pattern matching. > > > > Any ideas? > > > > Thanks in Advance, > > Richard > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Richard
2007-Feb-20 14:54 UTC
Re: Create a hyphen-separated set of letters derived from a string - How to?
Hi Folks, Thanks for your responses. They''re both great. After posting my question in frustration, I finally found approach in my copy of Hal Fulton''s book, as shown below. So my frustration''s ended and I''ve got three solutions! Again, many thanks. Regards, Richard # CharFromString.rb # Source: "The Ruby Way, 2nd. ed". p. 68 sString = "abc" asChars = sString.scan(/./) sSpacedString = String.new asChars.each { |sChar| sSpacedString << sChar << '' '' } puts sSpacedString This above is a more verbose approach than Hal Fulton''s. I like to use the "type" prefixes originally adopted by Charles Petzold, the lead developer of Windows. Best wishes, Richard On Feb 20, 1:24 am, "Richard" <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote:> Hi, > > This is such a trivial programming issue, but I can''t find a way to > transform, say ''abc'' to ''a-b-c'' without using pattern matching. > > Any ideas? > > Thanks in Advance, > Richard--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Isak Hansen
2007-Feb-20 15:52 UTC
Re: Create a hyphen-separated set of letters derived from a string - How to?
On 2/20/07, Richard <RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> > Hi Folks, > > Thanks for your responses. They''re both great. After posting my > question in frustration, I finally found approach in my copy of Hal > Fulton''s book, as shown below. So my frustration''s ended and I''ve got > three solutions! > > Again, many thanks. > > Regards, > Richard > > # CharFromString.rb > # Source: "The Ruby Way, 2nd. ed". p. 68 > > sString = "abc" > asChars = sString.scan(/./) > sSpacedString = String.new > asChars.each { |sChar| sSpacedString << sChar << '' '' } > puts sSpacedString >irb(main):001:0> "Here''s a one-liner".split(//).join "-" => "H-e-r-e-''-s- -a- -o-n-e---l-i-n-e-r"> This above is a more verbose approach than Hal Fulton''s. I like to use > the "type" prefixes originally adopted by Charles Petzold, the lead > developer of Windows. >Your coding conventions are quite far from the norm, yes. Have a look at <http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=RubyCodingConvention>, for instance. You''ll also find that most non-delphi programmers think hungarian notation is evil (type prefixes). Finally, I thought I''d point out that comp.lang.ruby (e.g. on google groups, and relayed to ruby-talk-X+L+6nJQZ58h9ZMKESR00Q@public.gmane.org) may be a better place for generic Ruby programming questions. HTH, Isak> Best wishes, > Richard > > On Feb 20, 1:24 am, "Richard" > <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote: > > Hi, > > > > This is such a trivial programming issue, but I can''t find a way to > > transform, say ''abc'' to ''a-b-c'' without using pattern matching. > > > > Any ideas? > > > > Thanks in Advance, > > Richard > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Richard
2007-Feb-21 05:28 UTC
Re: Create a hyphen-separated set of letters derived from a string - How to?
Hi Isak Thanks for responding.> irb(main):001:0> "Here''s a one-liner".split(//).join "-" > => "H-e-r-e-''-s- -a- -o-n-e---l-i-n-e-r"Nice! I thought of "join" this afternoon while I was driving to a Microsoft presentation on Vista & Office 2007. A very different world from RoR, eh?> Your coding conventions are quite far from the norm, yes. Have a look > at <http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=RubyCoding...>, > for instance.Thanks for the link. That''s handy.> You''ll also find that most non-Delphi programmers think Hungarian > notation is evil (type prefixes).I know there has been almost religious fervor on this issue. I find Hungarian notation helps me avoid having to search for variables'' most recent assignments in order to know what kind of objects they are referencing. I feel it helps me write code more accurately.> Finally, I thought I''d point out that comp.lang.ruby (e.g. on google > groups, and relayed to ruby-t...-X+L+6nJQZ58h9ZMKESR00Q@public.gmane.org) may be a better place > for generic Ruby programming questions.I agree. Actually, I''m really interested in Ruby nuances in order to write better RoR code, so I hang out on this group. Anyway, as Dave Black stated emphatically in Ruby For Rails, "Rails programs ARE Ruby programs" (or something to that effect.) But if I get more flack about it, I''ll do as you suggest. I wonder what you think of my approach of writing little demos expressing the nuances of various Ruby and Rails issues like the following (which is going to be obsolete in the next version of Ruby, I hear. That is, String[offset] will no longer return a Fixnum.) I like the following "applet" because I can run it months from now and recall at glance the issues exemplified therein. Best wishes, Richard # CharFromString.rb # Source: "The Ruby Way, 2nd. ed". p. 68 def show(stmt) print stmt puts "\n=> " + eval(stmt).inspect puts end show %@ sString = "abc" asChars = sString.scan(/./) asChars.join(''-'') # => a-b-c @ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---