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
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.
manveru@pi ~ % irb
comment = <<COMMENT
This is ruby code.
More lovely code.
Better than PHP.
That is it.
COMMENT
it.\n"
comment.scan(/\w+/).map{|w| w.downcase }.uniq
“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.
COMMENTresult_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.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs