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

**URL:** <https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148>\
**Category:** Rails\
**Created:** [February 20, 2007, 7:25am UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148 "2007-02-20T07:25:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 20, 2007, 7:25am UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/1 "2007-02-20T07:25:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 20, 2007, 9:37am UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/2 "2007-02-20T09:37:55Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 20, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/3 "2007-02-20T15:56:00Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 20, 2007, 10:12am UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/4 "2007-02-20T10:12:00Z")

</div>

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\>

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 20, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/5 "2007-02-20T19:46:37Z")

</div>

On 2/20/07, Richard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](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 removed\_email\_address@domain.invalid) may be a better place  
for generic Ruby programming questions.

HTH,  
Isak

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [February 21, 2007, 6:28am UTC](https://www.ruby-forum.com/t/create-a-hyphen-separated-set-of-letters-derived-from-a-stri/86148/6 "2007-02-21T06:28:49Z")

</div>

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...](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 removed\_email\_address@domain.invalid) 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  
@
