# Converting Strings

**URL:** <https://www.ruby-forum.com/t/converting-strings/126893>\
**Category:** Ruby\
**Created:** [January 23, 2008, 6:17am UTC](https://www.ruby-forum.com/t/converting-strings/126893 "2008-01-23T06:17:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenki99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f17d59/32.png) [@zenki99](https://www.ruby-forum.com/u/zenki99)\
**Post date:** [January 23, 2008, 6:17am UTC](https://www.ruby-forum.com/t/converting-strings/126893/1 "2008-01-23T06:17:33Z")

</div>

hey,

I’m trying to convert the strings below into an array of all the unique  
words converted to lower case.

comment \<\< COMMENT  
This is a ruby code.  
More lovely code.  
Better than PHP.  
That is it.  
COMMENT

result\_string = “”

Thanks

---

<div class="post-metadata">

**Author:** ![zenki99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f17d59/32.png) [@zenki99](https://www.ruby-forum.com/u/zenki99)\
**Post date:** [January 23, 2008, 7:32am UTC](https://www.ruby-forum.com/t/converting-strings/126893/2 "2008-01-23T07:32:33Z")

</div>

Ok:

comment =\<\<COMMENT  
This is a ruby code.  
More lovely code.  
Better than PHP.  
That is it.  
COMMENT

comment.gsub(/[-.,!:;’"]/, ‘’).split(/\s+/).map{|w|  
w.downcase}.uniq.sort

=\> [“a”, “better”, “code”, “is”, “it”, “lovely”, “more”, “php”,  
“ruby”, “than”, “that”, “this”]

---

<div class="post-metadata">

**Author:** ![zenki99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f17d59/32.png) [@zenki99](https://www.ruby-forum.com/u/zenki99)\
**Post date:** [January 23, 2008, 7:58am UTC](https://www.ruby-forum.com/t/converting-strings/126893/3 "2008-01-23T07:58:21Z")

</div>

On Jan 23, 2008 2:17 PM, Zenki N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey,
> 
> I’m trying to convert the strings below into an array of all the unique  
> words converted to lower case.

manveru@pi ~ % irb  
comment = \<\<COMMENT  
This is ruby code.  
More lovely code.  
Better than PHP.  
That is it.  
COMMENT

# "This is ruby code.\nMore lovely code.\nBetter than PHP.\nThat is

it.\n"  
comment.scan(/\w+/).map{|w| w.downcase }.uniq

# [“this”, “is”, “ruby”, “code”, “more”, “lovely”, “better”, “than”,

“php”, “that”, “it”]

---

<div class="post-metadata">

**Author:** ![zenki99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f17d59/32.png) [@zenki99](https://www.ruby-forum.com/u/zenki99)\
**Post date:** [January 23, 2008, 7:43am UTC](https://www.ruby-forum.com/t/converting-strings/126893/4 "2008-01-23T07:43:21Z")

</div>

Zenki N. wrote:

> hey,
> 
> I’m trying to convert the strings below into an array of all the unique  
> words converted to lower case.
> 
> comment \<\< COMMENT  
> This is a ruby code.  
> More lovely code.  
> Better than PHP.  
> That is it.  
> COMMENT
> 
> result\_string = “”
> 
> Thanks

Try this:

require ‘set’

comment = \<\<COMMENT  
This is ruby code.  
More lovely code.  
Better than PHP.  
That is it.  
COMMENT

lower = comment.downcase  
lines = lower.split(".\n")

unique\_words = Set.new()  
lines.each do |line|  
words = line.split()  
words.each do |word|  
unique\_words \<\< word  
end  
end

p unique\_words
