looking for capital words

A

arose

I want some ruby code that takes a string from the cmd prompt and does
one action if the word is capitalized and another if it is not.

What would you do?

Would you use regular expressions or is there some nifty Ruby methods
in the String class that would be better?
 
A

arose

arose said:
I want some ruby code that takes a string from the cmd prompt and does
one action if the word is capitalized and another if it is not.

What would you do?

Would you use regular expressions or is there some nifty Ruby methods
in the String class that would be better?

This is what i have so far:

if instructiontxt.capitalize then
puts "capitalized"
end
unless instructiontxt.capitalize then
puts "not capitalized"
end
 
D

Drew

arose said:
This is what i have so far:

if instructiontxt.capitalize then
puts "capitalized"
end
unless instructiontxt.capitalize then
puts "not capitalized"
end

I would use the string method capitalize and try something like

if str == str.capitalize
# do something
else
# do something else
end
 
D

Daniel Schierbeck

arose said:
I want some ruby code that takes a string from the cmd prompt and does
one action if the word is capitalized and another if it is not.

What would you do?

Would you use regular expressions or is there some nifty Ruby methods
in the String class that would be better?

Just a single alphabetic word?

if word =~ /^[A-Z][A-Za-z]*$/ then
puts "capitalized"
else
puts "not capitalized"
end

You could even put it in String:

class String
def capitalized?
self =~ /^[A-Z][a-zA-Z]*$/
end
end


Cheers,
Daniel
 
G

Gene Tani

arose said:
I want some ruby code that takes a string from the cmd prompt and does
one action if the word is capitalized and another if it is not.

What would you do?

Would you use regular expressions or is there some nifty Ruby methods
in the String class that would be better?

maybe look at some of the Highline to see how to handle line-oriented
input

http://www.rubyquiz.com/quiz29.html
 
A

arose

Jeffrey,

Thanks that is a definitel help, changing the way i look at these
problems too.

Where do you put the following?
class String
def capitalized?
self == self.capitalize
end
end
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top