Converting Strings

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

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”]

On Jan 23, 2008 2:17 PM, Zenki N. [email protected] wrote:

hey,

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

[email protected] ~ % 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”]

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

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