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?
On 4 Oct 2007, at 13:00, Lloyd L. wrote:
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-forum.com/.
How about:
File.foreach(‘\movies.txt’) do |l|
puts l.split.map{|w| w.capitalize}.join(" ")
end
Alex G.
Bioinformatics Center
Kyoto University
On 10/4/07, Lloyd L. [email protected] wrote:
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?
In rails, activesupport there is the titlize method
Capitalizes all the words and replaces some characters in the string
to
create
a nicer looking title. Titleize is meant for creating pretty output.
It is
not
used in the Rails internals.
titleize is also aliased as as titlecase
Examples
“man from the boondocks”.titleize #=> “Man From The Boondocks”
“x-men: the last stand”.titleize #=> “X Men: The Last Stand”
def titleize(word)
humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
end
So for your file you could use
File.open( ‘\movies.txt’) do |f|
f.each_line do |line|
puts line.gsub(/\b(a-z])/){ $1.capitalize }
end
end
Or if you don’t mind printing the whole thing out in one go
File.open( 'movies.txt’) do |f|
puts f.read.gsub(/\b([a-z]/){ $1.capitalize }
end
HTH
Daniel
2007/10/4, Alex G. [email protected]:
File.open(‘\movies.txt’) do |f|
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 – 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
2007/10/4, 7stud – [email protected]:
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 L. 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
Robert K. 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
What an education! That is just too cool! Thanks everyone!
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
2007/10/4, 7stud – [email protected]:
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
Robert K. wrote:
2007/10/4, 7stud – [email protected]:
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
On 04.10.2007 16:32, 7stud – wrote:
print word.capitalize
" "
"b "
“c”
=> “a b c”
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
Robert K. wrote:
On 04.10.2007 16:32, 7stud – wrote:
print word.capitalize
" "
"b "
“c”
=> “a b c”
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.
On Oct 3, 11:00 pm, Lloyd L. [email protected] wrote:
puts s.chop
end
end
puts IO.read(‘data’).gsub(/\w+/){ $&.capitalize }