Opinion on a simple method

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

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

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

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

HTH
Daniel

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-forum.com/\.

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

···

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

I'd probably do this:

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

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-forum.com/\.

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

···

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

What an education! That is just too cool! Thanks everyone!

···

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

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-forum.com/\.

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-forum.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. :frowning:

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

···

2007/10/4, 7stud -- <dolgun@excite.com>:

Robert Klemme wrote:

>

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

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

···

2007/10/4, 7stud -- <dolgun@excite.com>:

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

Robert Klemme wrote:

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

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. :slight_smile:

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

That's what I said. :slight_smile:

Kind regards

  robert

···

On 04.10.2007 16:32, 7stud -- wrote:

2007/10/4, 7stud -- <dolgun@excite.com>:

Robert Klemme 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. :slight_smile:

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 04.10.2007 16:32, 7stud -- wrote:

--
Posted via http://www.ruby-forum.com/\.