Create a hyphen-separated set of letters derived from a stri

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

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”

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 F.'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”

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 2/20/07, Richard [email protected]
wrote:

Regards,

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 F.'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 [email protected]) may be a better place
for generic Ruby programming questions.

HTH,
Isak

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 [email protected]) 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
@

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs