File attached.
I need some help with a simple program to capitalize the first letter of
each word in a sentence.
I'm not sure if I should be using class Array, but I dont think that's
the problem.
Here's the code:
class Array
def title_format (title)
puts title.split(" ").each{|element| print
element.capitalize!}.join(" ")
return title_format
end
end
puts "Please type the sentence you want to have put in \"Title Format\""
title = gets.chomp
title.title_format
Help would be greatly appreciated. This is the error i get:
sentence_caps.rb:18: undefined method `title_format' for "please help me
with my program":String (NoMethodError)
Thanks
Attachments:
http://www.ruby-forum.com/attachment/6641/sentence_caps.rb
···
--
Posted via http://www.ruby-forum.com/.
Which is exactly right -- 'title' is a string and there's no such method.
But you can add one:
class String
def title_format
self.split.each{|element| element.capitalize! }.join(" ")
end
end
puts "what the heck".title_format #=> What The Heck
You definitely don't want the `print` and `puts` inside your method,
and in your example you're trying to return the method itself, which
is bound to end badly
HTH,
···
On Thu, Sep 29, 2011 at 3:19 PM, Trevor Harker <koolaidmancometh@gmail.com> wrote:
I need some help with a simple program to capitalize the first letter of
each word in a sentence.
class Array
def title_format (title)
puts title.split(" ").each{|element| print
element.capitalize!}.join(" ")
return title_format
end
end
title.title_format
Help would be greatly appreciated. This is the error i get:
sentence_caps.rb:18: undefined method `title_format' for "please help me
with my program":String (NoMethodError)
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
What Hassan said, but something to consider is that this will change your
string in ways you don't necessarily want.
class String
def title_format
self.split.each{|element| element.capitalize! }.join(" ")
end
end
string = "unexpected\n \tWhitespace"
string.title_format # => "Unexpected Whitespace"
# instead, I'd do it like this, which substitutes each run of non-whitespace
characters
# and substitutes them with their capitalized version
class String
def title_format
gsub(/\S+/) { |word| word.capitalize }
# which can actually be shortened to: gsub(/\S+/, &:capitalize)
end
end
string.title_format # => "Unexpected\n \tWhitespace"
···
On Thu, Sep 29, 2011 at 5:19 PM, Trevor Harker <koolaidmancometh@gmail.com>wrote:
File attached.
I need some help with a simple program to capitalize the first letter of
each word in a sentence.
I'm not sure if I should be using class Array, but I dont think that's
the problem.
Here's the code:
class Array
def title_format (title)
puts title.split(" ").each{|element| print
element.capitalize!}.join(" ")
return title_format
end
end
puts "Please type the sentence you want to have put in \"Title Format\""
title = gets.chomp
title.title_format
Help would be greatly appreciated. This is the error i get:
sentence_caps.rb:18: undefined method `title_format' for "please help me
with my program":String (NoMethodError)
Thanks
Attachments:
http://www.ruby-forum.com/attachment/6641/sentence_caps.rb
--
Posted via http://www.ruby-forum.com/\.
http://snippets.dzone.com/posts/show/4702
'some string here'.gsub(/\b\w/){$&.upcase}
title.gsub(/\b\w/){$&.upcase}
···
On 9/29/2011 6:19 PM, Trevor Harker wrote:
File attached.
I need some help with a simple program to capitalize the first letter of
each word in a sentence.
Excellent point, thanks for that caveat.
Sheesh, old web guys -- so cavalier about white space
···
On Thu, Sep 29, 2011 at 5:04 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
What Hassan said, but something to consider is that this will change your
string in ways you don't necessarily want.
string = "unexpected\n \tWhitespace"
string.title_format # => "Unexpected Whitespace"
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan