opinion on a simple method

  • Thread starter Lloyd Linklater
  • Start date
L

Lloyd Linklater

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?
 
A

Alex Gutteridge

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?

How about:

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

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
R

Robert Klemme

2007/10/4 said:
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
 
7

7stud --

Lloyd said:
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
 
7

7stud --

7stud said:
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
 
R

Robert Klemme

2007/10/4 said:
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
 
7

7stud --

Robert said:
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
 
7

7stud --

7stud said:
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
 
R

Robert Klemme

2007/10/4 said:
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
 
7

7stud --

Robert said:
I don't think so. Please look again!


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
 
W

William James

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

puts IO.read('data').gsub(/\w+/){ $&.capitalize }
 
R

Robert Klemme

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

I just wanted to point out that there might be some useless
capitalizations going on because of the white space. My solution does
not do that because it capitalizes words only. If you will, it's just
an aesthetic improvement. :)
because my original solution and your solution result in the same
output:

That's what I said. :)

Kind regards

robert
 
7

7stud --

Robert said:
I just wanted to point out that there might be some useless
capitalizations going on because of the white space. My solution does
not do that because it capitalizes words only. If you will, it's just
an aesthetic improvement. :)

I'm not sure exactly how the internals of capitalize work, but based on
its output it does not blindly subtract 35 from the ascii code for the
first character of a word. So, internally capitalize sorts out which
characters to capitalize and which characters to leave alone(e.g.
capital letters, punctuation, spaces).

It is possible to use ruby to skip the useless call to capitalize for
words that are spaces, like this:

if word == " "
print word
else
print word.capitalize
end

but it turns out that(on my system at least) using that if statement is
fractionally slower than letting the C code in the capitalize method
take care of words that are spaces.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top