arose
6/12/2006 6:09:00 PM
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.