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.foreach('\movies.txt') do |l|
puts l.split.map{|w| w.capitalize}.join(" ")
end
Alex Gutteridge
Bioinformatics Center
Kyoto University
···
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
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.
···
On 10/4/07, Lloyd Linklater <lloyd@2live4.com> 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?
--
#
# 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
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
On Oct 3, 11:00 pm, Lloyd Linklater <ll...@2live4.com> 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
File.foreach("movies.txt") do |line|
puts line.gsub(/\w+/) {|word| word.capitalize}
end
Kind regards
robert
···
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-forum.com/\.
>
How about:
File.foreach('\movies.txt') do |l|
puts l.split.map{|w| w.capitalize}.join(" ")
end
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"
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:
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.