[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

looking for capital words

arose

6/10/2006 7:48:00 PM

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?

7 Answers

arose

6/10/2006 7:53:00 PM

0


arose wrote:
> 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

Drew

6/10/2006 8:19:00 PM

0


arose wrote:
> arose wrote:
> > 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

I would use the string method capitalize and try something like

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

Jeff Schwab

6/10/2006 10:16:00 PM

0

arose wrote:
> arose wrote:
>> 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?

Depends on the situation.

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

Probably not, and yes, respectively.

> This is what i have so far:
>
> if instructiontxt.capitalize then
> puts "capitalized"
> end

The String#capitalize method does not tell you whether the string is
already capitalized, but returns a capitalized version of the string.
Methods that only check whether an object satisfies a particular
condition are called predicates, and in Ruby typically end in questions
marks (?). Here is how you might implement an appropriate predicate:

class String
def capitalized?
self == self.capitalize
end
end

You could use the predicate like this:

def print_whether_capitalized(s)
print s + ": "
print "not " unless s.capitalized?
puts "capitalized"
end

print_whether_capitalized('This string is capitalized ')
print_whether_capitalized('but this one is not')
print_whether_capitalized('Not Capitalized, According To Ruby')

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

Your code is structured like this:

if condition
do something
end
unless condition
do something else
end

A more easily maintained alternative is to use an if-else:

if condition
do something
else
do something else
end

Good luck.


Daniel Schierbeck

6/10/2006 10:57:00 PM

0

arose wrote:
> 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

Gene Tani

6/11/2006 8:33:00 AM

0


arose wrote:
> 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/q...

arose

6/12/2006 5:59:00 PM

0

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


Jeffrey Schwab wrote:
> arose wrote:
> > arose wrote:
> >> 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?
>
> Depends on the situation.
>
> >> Would you use regular expressions or is there some nifty Ruby methods
> >> in the String class that would be better?
>
> Probably not, and yes, respectively.
>
> > This is what i have so far:
> >
> > if instructiontxt.capitalize then
> > puts "capitalized"
> > end
>
> The String#capitalize method does not tell you whether the string is
> already capitalized, but returns a capitalized version of the string.
> Methods that only check whether an object satisfies a particular
> condition are called predicates, and in Ruby typically end in questions
> marks (?). Here is how you might implement an appropriate predicate:
>
> class String
> def capitalized?
> self == self.capitalize
> end
> end
>
> You could use the predicate like this:
>
> def print_whether_capitalized(s)
> print s + ": "
> print "not " unless s.capitalized?
> puts "capitalized"
> end
>
> print_whether_capitalized('This string is capitalized ')
> print_whether_capitalized('but this one is not')
> print_whether_capitalized('Not Capitalized, According To Ruby')
>
> > unless instructiontxt.capitalize then
> > puts "not capitalized"
> > end
>
> Your code is structured like this:
>
> if condition
> do something
> end
> unless condition
> do something else
> end
>
> A more easily maintained alternative is to use an if-else:
>
> if condition
> do something
> else
> do something else
> end
>
> Good luck.

arose

6/12/2006 6:09:00 PM

0

Never mind, I had the word Class capitalized, my bad.



arose wrote:
> 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
>
>
> Jeffrey Schwab wrote:
> > arose wrote:
> > > arose wrote:
> > >> 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?
> >
> > Depends on the situation.
> >
> > >> Would you use regular expressions or is there some nifty Ruby methods
> > >> in the String class that would be better?
> >
> > Probably not, and yes, respectively.
> >
> > > This is what i have so far:
> > >
> > > if instructiontxt.capitalize then
> > > puts "capitalized"
> > > end
> >
> > The String#capitalize method does not tell you whether the string is
> > already capitalized, but returns a capitalized version of the string.
> > Methods that only check whether an object satisfies a particular
> > condition are called predicates, and in Ruby typically end in questions
> > marks (?). Here is how you might implement an appropriate predicate:
> >
> > class String
> > def capitalized?
> > self == self.capitalize
> > end
> > end
> >
> > You could use the predicate like this:
> >
> > def print_whether_capitalized(s)
> > print s + ": "
> > print "not " unless s.capitalized?
> > puts "capitalized"
> > end
> >
> > print_whether_capitalized('This string is capitalized ')
> > print_whether_capitalized('but this one is not')
> > print_whether_capitalized('Not Capitalized, According To Ruby')
> >
> > > unless instructiontxt.capitalize then
> > > puts "not capitalized"
> > > end
> >
> > Your code is structured like this:
> >
> > if condition
> > do something
> > end
> > unless condition
> > do something else
> > end
> >
> > A more easily maintained alternative is to use an if-else:
> >
> > if condition
> > do something
> > else
> > do something else
> > end
> >
> > Good luck.