Dear JD,

Perhaps you can get to something really simpler than that, like ...

"Take the title and globally substitute each word matched with the
capitalized match"

@title =3D title.gsub(/\w+/) { |match| match.capitalize }

;-)

Complete code to pass the test would be something like:

class Name

  attr_reader :title

  def title=3D(title)
    @title =3D title.gsub(/\w+/) { |match| match.capitalize }
  end
end

Abinoam Jr.

On Mon, Jan 20, 2014 at 6:12 AM, Jes=FAs Gabriel y Gal=E1n
<jgabrielygalan / gmail.com> wrote:
> On Mon, Jan 20, 2014 at 3:35 AM, JD JD <lists / ruby-forum.com> wrote:
>> Also, something else that confuses me a bit is the need for the @title2.
>> I noticed if if I just make them regular variables (aka, title2 without
>> the @) the code will not pass anymore.
>>
>> I understand what the @ symbols are for.  They are instance variables
>> (unless again using wrong termonology) for that class.  I can understand
>> the point of them with the initialize method.
>>
>> But, why is it necessary in the def name method?
>>
>> Sorry, I guess I know how to make the code work by typing all that out,
>> but I guess I don't understand the "why" behind what I did.
>
> You shouldn't need to use instance variables if you only use them
> within one method. So a local variable should work. Can you show the
> code and the test that doesn't pass?
>
> Also, some little comments:
>
> def title
>     array_words=3D%w(and of in the a an)
>
> # array_words is not a good name, stop_words or something like that
> would be better
> # also to check for inclusion, a Set would be more efficient
>
>     @title2=3D / title.scan(/\w+/)
>     word_number=3D0
>     @title2.each do |title|
>
> # instead of word_number you could use each_with_index. It will
> maintain the index for you
> # also, using title as the local variable of the block is confusing, I
> would call it word, for example
>
>       if array_words.include?(title) && word_number >=3D 1
>         title
>         word_number+=3D1
>       else
>         title.capitalize!
>         word_number+=3D1
>       end
>     end
>     @title2.join(" ")
>   end
>
>
> Jesus.