[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Converting Strings

Zenki Nine

1/23/2008 5:17:00 AM

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
--
Posted via http://www.ruby-....

3 Answers

Steve Ross

1/23/2008 6:32:00 AM

0

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 22, 2008, at 9:17 PM, Zenki Nine wrote:

> This is a ruby code.
> More lovely code.
> Better than PHP.
> That is it.
> COMMENT


7stud --

1/23/2008 6:43:00 AM

0

Zenki Nine 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
--
Posted via http://www.ruby-....

Michael Fellinger

1/23/2008 6:58:00 AM

0

On Jan 23, 2008 2:17 PM, Zenki Nine <amateurdesigner@gmail.com> 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"]