[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

opinion on a simple method

Lloyd Linklater

10/4/2007 4:00:00 AM

I am trying to find the better way to do things ruby style. I needed to
make a method that would read in a file of movie titles and capitalize
each word.

"this is a string".capitalize just gets the first word, so I did this:

File.open('\movies.txt') do |f|
while line = f.gets
s = ""
line.split(/ /).each {|one_word| s += one_word.capitalize + ' '}
puts s.chop
end
end

Is there a cleaner or more "rubyish" way to do this?
--
Posted via http://www.ruby-....

13 Answers

Alex Gutteridge

10/4/2007 4:23:00 AM

0


On 4 Oct 2007, at 13:00, Lloyd Linklater wrote:

> I am trying to find the better way to do things ruby style. I
> needed to
> make a method that would read in a file of movie titles and capitalize
> each word.
>
> "this is a string".capitalize just gets the first word, so I did this:
>
> File.open('\movies.txt') do |f|
> while line = f.gets
> s = ""
> line.split(/ /).each {|one_word| s += one_word.capitalize + ' '}
> puts s.chop
> end
> end
>
> Is there a cleaner or more "rubyish" way to do this?
> --
> Posted via http://www.ruby-....
>

How about:

File.foreach('\movies.txt') do |l|
puts l.split.map{|w| w.capitalize}.join(" ")
end

Alex Gutteridge

Bioinformatics Center
Kyoto University



Robert Klemme

10/4/2007 8:37:00 AM

0

2007/10/4, Alex Gutteridge <alexg@kuicr.kyoto-u.ac.jp>:
>
> On 4 Oct 2007, at 13:00, Lloyd Linklater wrote:
>
> > I am trying to find the better way to do things ruby style. I
> > needed to
> > make a method that would read in a file of movie titles and capitalize
> > each word.
> >
> > "this is a string".capitalize just gets the first word, so I did this:
> >
> > File.open('\movies.txt') do |f|
> > while line = f.gets
> > s = ""
> > line.split(/ /).each {|one_word| s += one_word.capitalize + ' '}
> > puts s.chop
> > end
> > end
> >
> > Is there a cleaner or more "rubyish" way to do this?
> > --
> > Posted via http://www.ruby-....
> >
>
> How about:
>
> File.foreach('\movies.txt') do |l|
> puts l.split.map{|w| w.capitalize}.join(" ")
> end

I'd probably do this:

File.foreach("movies.txt") do |line|
puts line.gsub(/\w+/) {|word| word.capitalize}
end

Kind regards

robert

7stud 7stud

10/4/2007 10:08:00 AM

0

Lloyd Linklater wrote:
> I am trying to find the better way to do things ruby style. I needed to
> make a method that would read in a file of movie titles and capitalize
> each word.
>
> "this is a string".capitalize just gets the first word, so I did this:
>
> File.open('\movies.txt') do |f|
> while line = f.gets
> s = ""
> line.split(/ /).each {|one_word| s += one_word.capitalize + ' '}
> puts s.chop
> end
> end
>
> Is there a cleaner or more "rubyish" way to do this?


File.open("data.txt") do |file|
file.each() do |line|
line.each(" ") do |word|
print word.capitalize
end
end
end


--input:--
the Bourne ultimatum
harry Potter and the Order of the phoenix
mission impossible

--output:--
The Bourne Ultimatum
Harry Potter And The Order Of The Phoenix
Mission Impossible


--
Posted via http://www.ruby-....

7stud 7stud

10/4/2007 10:19:00 AM

0

7stud -- wrote:
>
> File.open("data.txt") do |file|
> file.each() do |line|
> line.each(" ") do |word|
> print word.capitalize
> end
> end
> end
>

Ahh. And stealing from Alex and Robert:

IO.foreach("data.txt") do |line|
line.each(" ") do |word|
print word.capitalize
end
end
--
Posted via http://www.ruby-....

Robert Klemme

10/4/2007 11:18:00 AM

0

2007/10/4, 7stud -- <dolgun@excite.com>:
> 7stud -- wrote:
> >
> > File.open("data.txt") do |file|
> > file.each() do |line|
> > line.each(" ") do |word|
> > print word.capitalize
> > end
> > end
> > end
> >
>
> Ahh. And stealing from Alex and Robert:
>
> IO.foreach("data.txt") do |line|
> line.each(" ") do |word|
> print word.capitalize
> end
> end

Nice idea but:

irb(main):001:0> "a b c".each(" ") {|w| p w}
"a "
" "
"b "
"c"
=> "a b c"

Note, it'll probably still work.

Kind regards

robert

Lloyd Linklater

10/4/2007 11:56:00 AM

0

What an education! That is just too cool! Thanks everyone!
--
Posted via http://www.ruby-....

7stud 7stud

10/4/2007 12:15:00 PM

0

Robert Klemme wrote:
>
> irb(main):001:0> "a b c".each(" ") {|w| p w}
> "a "
> " "
> "b "
> "c"
> => "a b c"
>
> Note, it'll probably still work.
>

Nice catch! Actually, your solution suffers from the same probem. :(

Adding this line corrects mine, and it doesn't slow it down much:

IO.foreach("data.txt") do |line|
line.each(" ") do |word|
next if word == " "
print word.capitalize
end
end
--
Posted via http://www.ruby-....

7stud 7stud

10/4/2007 12:17:00 PM

0

7stud -- wrote:
>
> Adding this line corrects mine, and it doesn't slow it down much:
>
> IO.foreach("data.txt") do |line|
> line.each(" ") do |word|
> next if word == " "
> print word.capitalize
> end
> end

--input:--
the Bourne ultimatum
harry Potter and the Order of the phoenix
mission impossible

--output:--
The Bourne Ultimatum
Harry Potter And The Order Of The Phoenix
Mission Impossible
--
Posted via http://www.ruby-....

Robert Klemme

10/4/2007 12:27:00 PM

0

2007/10/4, 7stud -- <dolgun@excite.com>:
> Robert Klemme wrote:
> >
> > irb(main):001:0> "a b c".each(" ") {|w| p w}
> > "a "
> > " "
> > "b "
> > "c"
> > => "a b c"
> >
> > Note, it'll probably still work.
> >
>
> Nice catch! Actually, your solution suffers from the same probem. :(

I don't think so. Please look again!

> Adding this line corrects mine, and it doesn't slow it down much:
>
> IO.foreach("data.txt") do |line|
> line.each(" ") do |word|
> next if word == " "
> print word.capitalize
> end
> end

But it changes white spaces - which my solution does not do.

Kind regards

robert

7stud 7stud

10/4/2007 2:33:00 PM

0

Robert Klemme wrote:
> 2007/10/4, 7stud -- <dolgun@excite.com>:
>> >
>>
>> Nice catch! Actually, your solution suffers from the same probem. :(
>
> I don't think so. Please look again!
>
>> Adding this line corrects mine, and it doesn't slow it down much:
>>
>> IO.foreach("data.txt") do |line|
>> line.each(" ") do |word|
>> next if word == " "
>> print word.capitalize
>> end
>> end
>
> But it changes white spaces - which my solution does not do.
>

Then, I'm not sure what you were pointing out here:

>
>Nice idea but:
>
>irb(main):001:0> "a b c".each(" ") {|w| p w}
>"a "
>" "
>"b "
>"c"
>=> "a b c"

because my original solution and your solution result in the same
output:


IO.foreach("data.txt") do |line|
line.each(" ") do |word|
print word.capitalize
end
end

puts "*********"

File.foreach("data.txt") do |line|
puts line.gsub(/\w+/) {|word| word.capitalize}
end


--input:---
the Bourne ultimatum
harry Potter and the Order of the phoenix
mission impossible


--output:--
The Bourne Ultimatum
Harry Potter And The Order Of The Phoenix
Mission Impossible
**********
The Bourne Ultimatum
Harry Potter And The Order Of The Phoenix
Mission Impossible
--
Posted via http://www.ruby-....