Converting Strings

Z

Zenki Nine

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
 
S

s.ross

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

7stud --

Zenki said:
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
 
M

Michael Fellinger

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,277
Messages
2,571,385
Members
48,087
Latest member
DomenicGib

Latest Threads

Top