--047d7bf0e80002b67f04f2b3be08
Content-Type: text/plain; charset=ISO-8859-1

This code is the reason for your error
@allwords  allwords + aword.reverse.capitalize + " "

Since @allwords has no value in it, by default that instance variable is
initialized to nil. Thus creating this error. You need to add @allwords "" before the loop.

 @allwords  "
 @fullname.each do |aword|
        @allwords  allwords + aword.reverse.capitalize + " "
 end

There is a simpler method to do the same thing
without initializing @allwords

return @fullname.map { |word| word.reverse.capitalize }.join(' ' )

the above could would yeild the same result.


On Wed, Feb 19, 2014 at 1:19 AM, Paul Robinson <lists / ruby-forum.com> wrote:

> Hello, I'm just setting out on Ruby and my first little program is
> generating an error (in the line @allwords  allwords +
> aword.reverse.capitalize + " ")
>
> I'm trying to reverse a name, so it someone enters 'Fred' is will supply
> 'Derf' and if they enter 'Fred Smith' it will supply 'Derf Htims'
>
> Here's the code in full:
>
>
> class Jumbler
>   #attr_accessor :fullname
>   def initialize(fullname)
>     @fullname  ullname
>   end
>
>   def jumblename
>     #if there is more than one word, reverse each one
>     if @fullname.split(" ").length > 1
>       @fullname  fullname.split(" ")
>       @fullname.each do |aword|
>         @allwords  allwords + aword.reverse.capitalize + " "
>       end
>       return  @allwords
>     else
>       #just send back the single word
>       return @fullname.reverse.capitalize
>     end
>   end
> end
>
> puts "Please enter your name:"
> $stdout.flush
> yourname  ets.chomp
>
> jumbleyou  umbler.new(yourname)
> puts jumbleyou.jumblename
>
>
> I'm not sure if instance variables that are private to the object should
> appear in the initialization method (or whether they need to be denoted
> with '@' but that doesn't seen to fix the problem anyway). It seems to
> me that the error is because I'm trying to use 'reverse' or 'capitalize'
> on a 'nil' value but I can't see why that's happening.
>
> Guidance appreciated, sorry if my code is offensive! :D
>
> Paul
>
> --
> Posted via http://www.ruby-forum.com/.
>



-- 
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

--047d7bf0e80002b67f04f2b3be08
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir="ltr"><div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default">This code is the reason for your error/div><div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default">

<span style="font-family:arial,sans-serif;font-size:13px">@allwords = @allwords + aword.reverse.capitalize + &quot; &quot;</span></div><div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default">

<br></div><div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default">Since @allwords has no value in it, by default that instance variable is initialized to nil. Thus creating this error. You need to add @allwords = &quot;&quot; before the loop./div>

<div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;color:rgb(102,102,102)" class="gmail_default">   形⨱ 

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">  形⨳庱形⨳庱   形⨳庱

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"> 伯򾼯形⨱ 򾼯 

<font face="arial, sans-serif">There is a simpler method to do the same thing without  徼形 梾򾼯

<font face="arial, sans-serif">return @fullname.map { |word| word.reverse.capitalize }.join(&#39; &#39; )</font></div><div class="gmail_default" style><font face="arial, sans-serif"><br></font></div><div class="gmail_default" style>

<font face="arial, sans-serif">the above could would yeild the same result./font></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Feb 19, 2014 at 1:19 AM, Paul Robinson <span dir="ltr">&lt;<a href="mailto:lists / ruby-forum.com" target="_blank">lists / ruby-forum.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello, I&#39;m just setting out on Ruby and my first little program is<br>
generating an error (in the line @allwords = @allwords +<br>
aword.reverse.capitalize + &quot; &quot;)<br>
<br>
I&#39;m trying to reverse a name, so it someone enters &#39;Fred&#39; is will supply<br>
&#39;Derf&#39; and if they enter &#39;Fred Smith&#39; it will supply &#39;Derf Htims&#39;<br>
<br>
Here&#39;s the code in full:<br>
<br>
<br>
class Jumbler<br>
#attr_accessor :fullname<br>
def initialize(fullname)<br>
@fullname = fullname<br>
end<br>
<br>
def jumblename<br>
#if there is more than one word, reverse each one<br>
if @fullname.split(&quot; &quot;).length &gt; 1<br>
@fullname = @fullname.split(&quot; &quot;)<br>
@fullname.each do |aword|<br>
@allwords = @allwords + aword.reverse.capitalize + &quot;quot;<br>
end<br>
return  
else<br>
#just send back the single word<br>
return @fullname.reverse.capitalize<br>
end<br>
end<br>
end<br>
<br>
puts &quot;Please enter your name:&quot;<br>
$stdout.flush<br>
yourname = gets.chomp<br>
<br>
jumbleyou = Jumbler.new(yourname)<br>
puts jumbleyou.jumblename<br>
<br>
<br>
I&#39;m not sure if instance variables that are private to the object should<br>
appear in the initialization method (or whether they need to be denoted<br>
with &#39;@&#39; but that doesn&#39;t seen to fix the problem anyway). It seems to<br>
me that the error is because I&#39;m trying to use &#39;reverse&#39; or &#39;capitalize&#39;<br>
on a &#39;nil&#39; value but I can&#39;t see why that&#39;s happening.<br>
<br>
Guidance appreciated, sorry if my code is offensive! :D<br>
<span class="HOEnZb"><font color="#888888"><br>
Paul<br>
<br>
--<br>
Posted via <a href="http://www.ruby-forum.com/" target="_blank">http://www.ruby-forum.com/</a>.<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><span style="color:rgb(102,102,102);font-family:tahoma,sans-serif">Harisankar P S<br></span><span style="color:rgb(102,102,102);font-family:tahoma,sans-serif"><a href="https://twitter.com/coder_hsps" target="_blank">https://twitter.com/coder_hsps</a> ¯形⨱梾潢𺯯 뢾𺯯ᾼ形⨱
</div>
</div>

--047d7bf0e80002b67f04f2b3be08--