Having isues with capitalizing words

So, this is a method I have made:

def title(words)
    words.gsub(/(\A|\s)\w/) do |word|
  if(word!="and" || word!="an" || word!="the")
      word.upcase
  else
      word.downcase
  end
    end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

···

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

So, this is a method I have made:

def title(words)
     words.gsub(/(\A|\s)\w/) do |word|
   if(word!="and" || word!="an" || word!="the")
       word.upcase
   else
       word.downcase
   end
     end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

Does this help?

> irb
1.9.3p125 :001 > word = 'and'
  => "and"
1.9.3p125 :002 > word!="and" || word!="an" || word!="the"
  => true

Sam

···

On 12/06/2012 05:02 PM, JD KF wrote:

Try using "and"s instead of "or"s.

···

On 6 December 2012 14:02, JD KF <lists@ruby-forum.com> wrote:

So, this is a method I have made:

def title(words)
    words.gsub(/(\A|\s)\w/) do |word|
  if(word!="and" || word!="an" || word!="the")
      word.upcase
  else
      word.downcase
  end
    end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

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

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

Basically what sagy said helped me solve this. Thanks for the help
everyone :).

···

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

Your code won’t work even after replacing “or” with “and”s:

Firstly, don’t use “||” for “or”. Simply use “or” command.

Secondly, the capture group you defined (\A|\s) will catch only the
*first*letter. Hence it will never be “and” , “an” or “the”. Maybe
just “a” or “t”.

This will work:

*words.gsub!(/\w+/)do |match|*

* (match=="and" or match=="an" or match=="the") ? match : match.capitalize

···

*

*end*

Enjoy

Sagy

*From:* phluid61@gmail.com [mailto:phluid61@gmail.com] *On Behalf Of *Matthew
Kerwin
*Sent:* Thursday, December 06, 2012 6:11 AM
*To:* ruby-talk ML
*Subject:* Re: Having isues with capitalizing words

Try using "and"s instead of "or"s.

On 6 December 2012 14:02, JD KF <lists@ruby-forum.com> wrote:

So, this is a method I have made:

def title(words)
    words.gsub(/(\A|\s)\w/) do |word|
  if(word!="and" || word!="an" || word!="the")
      word.upcase
  else
      word.downcase
  end
    end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

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

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

or...

1.9.3-p194 :062 > phrase="the quick brown fox or a rabbit in the hen house"
=> "the quick brown fox or a rabbit in the hen house"
1.9.3-p194 :064 > title=phrase.split.each{|w| unless %{and or a
the}.include?(w) then w[0]=w[0].upcase end; w}.join(" ")
=> "the Quick Brown Fox or a Rabbit In the Hen House"
1.9.3-p194 :065 > title[0]=title[0].upcase
=> "T"
1.9.3-p194 :066 > title
=> "The Quick Brown Fox or a Rabbit In the Hen House"

···

On Wed, Dec 5, 2012 at 10:10 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Try using "and"s instead of "or"s.

On 6 December 2012 14:02, JD KF <lists@ruby-forum.com> wrote:

So, this is a method I have made:

def title(words)
    words.gsub(/(\A|\s)\w/) do |word|
  if(word!="and" || word!="an" || word!="the")
      word.upcase
  else
      word.downcase
  end
    end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

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

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

Combine sagy's and mine:

words.gsub!(/\w+/)do |match|
   %w{and or a an the of}.includes?(match) ? match : match.capitalize
end

(or just use gsub without the ! if you want to work on a copy instead
of original)

···

On Thu, Dec 6, 2012 at 12:39 AM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Wed, Dec 5, 2012 at 10:10 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Try using "and"s instead of "or"s.

On 6 December 2012 14:02, JD KF <lists@ruby-forum.com> wrote:

So, this is a method I have made:

def title(words)
    words.gsub(/(\A|\s)\w/) do |word|
  if(word!="and" || word!="an" || word!="the")
      word.upcase
  else
      word.downcase
  end
    end
end

I have been trying to solve this for the longest time and I can't figure
it out.

All I want the method to do is take in a string and capitalize every
word except for "and" and "an" and "the".

The above will not do it and I really don't understand why. I have
tried a million versions of it as well.

Can someone help me get in the right direction with this?

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

--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

or...

1.9.3-p194 :062 > phrase="the quick brown fox or a rabbit in the hen house"
=> "the quick brown fox or a rabbit in the hen house"
1.9.3-p194 :064 > title=phrase.split.each{|w| unless %{and or a
the}.include?(w) then w[0]=w[0].upcase end; w}.join(" ")
=> "the Quick Brown Fox or a Rabbit In the Hen House"
1.9.3-p194 :065 > title[0]=title[0].upcase
=> "T"
1.9.3-p194 :066 > title
=> "The Quick Brown Fox or a Rabbit In the Hen House"