Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby beginner Problem

Reply
Thread Tools

Ruby beginner Problem

 
 
Zayd Connor
Guest
Posts: n/a
 
      03-01-2009
I get the following error message when running the code below:
undefine method 'bottles _of_beer' for #<Bottles:0x2be1e1c>
(NoMethodError)

# To change this template, choose Tools | Templates
# and open the template in the editor.

class Bottles
def initialize (bottles_of_beer, bottle_word, one_word)
@bottles_of_beer = bottles_of_beer
@bottle_word = bottle_word
@one_word = one_word

end

my_bottles = Bottles.new(99,'Bottles','Bottle')

while my_bottles.bottles_of_beer >= 2
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
beer on the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"

else
puts "#{my.bottles.bottles_of_beer} #{my_bottles.bottle_word}of beer
on the wall"

end


if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer on
the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

puts "No more #{my_bottles.bottle_word} of beer on the wall"

end

end

end


>> Yeah I know this code is horribly formatted. I'm using Vim text editor, and have not yet figured out how to auto format my code.
>> What am I doing wrong? I still have a very long way to go, and practice alot more.


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

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-01-2009
Zayd Connor wrote:

> I get the following error message when running the code below:
> undefine method 'bottles _of_beer' for #<Bottles:0x2be1e1c>
> (NoMethodError)


> class Bottles
> def initialize (bottles_of_beer, bottle_word, one_word)

^

Ruby is space sensitive between the end of a method name and its first
parameter. You gotta take that space out.

In general, I don't know what other languages and editors you have learned, but
Ruby is very easy to format correctly via muscle memory. Just set your tabs to 2
spaces (no \t tab characters), and turn on the autoindent. From there, keeping
everything correctly formatted is quite simple, and this will lead, in turn, to
fewer syntax errors.

I remember a Visual Basic Classic that would have removed that space, for
example, but I know no Ruby editor which will does that for you!

--
Phlip
 
Reply With Quote
 
 
 
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-01-2009
[Note: parts of this message were removed to make it a legal post.]

Whoa, thats all to it, I need to get my formatting down right now =)
This is my first language so I have a long way to travel now this is
something I will be aware of in the future.

Thank you

On Sun, Mar 1, 2009 at 12:28 AM, Phlip <(E-Mail Removed)> wrote:

> Zayd Connor wrote:
>
> I get the following error message when running the code below:
>> undefine method 'bottles _of_beer' for #<Bottles:0x2be1e1c>
>> (NoMethodError)
>>

>
> class Bottles
>> def initialize (bottles_of_beer, bottle_word, one_word)
>>

> ^
>
> Ruby is space sensitive between the end of a method name and its first
> parameter. You gotta take that space out.
>
> In general, I don't know what other languages and editors you have learned,
> but Ruby is very easy to format correctly via muscle memory. Just set your
> tabs to 2 spaces (no \t tab characters), and turn on the autoindent. From
> there, keeping everything correctly formatted is quite simple, and this will
> lead, in turn, to fewer syntax errors.
>
> I remember a Visual Basic Classic that would have removed that space, for
> example, but I know no Ruby editor which will does that for you!
>
> --
> Phlip
>
>


 
Reply With Quote
 
lasitha
Guest
Posts: n/a
 
      03-01-2009
On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <(E-Mail Removed)> wrote:
> I'm using Vim text editor, and have not yet figured out how to auto format my code.


:h =

In Normal mode, hitting == will format the current line.
It takes the usual motion prefixes, so e.g. 5== will format the next 5 lines.
In Visual mode, = will format the selection.

This should work for any recognized file type. Most standard vim
installs include the ruby filetype plugin. If yours doesn't or needs
upgrading, see:
http://tinyurl.com/dd2ef9

Solidarity,
lasitha.

 
Reply With Quote
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-01-2009
[Note: parts of this message were removed to make it a legal post.]

Thanks Lasitha that worked perfect. I'm slowly getting more comfortable with
just the basic
I'm still getting that same error when compiling my code. I know its right
there in front of my face I just can't put my finger on it

Kindest Regards
Zayd


class Bottles
def initialize(bottles_of_beer, bottle_word, one_word)
@bottles_of_beer = bottles_of_beer
@bottle_word = bottle_word
@one_word = one_word

end


my_bottles = Bottles.new(99,'Bottles','Bottle')


while my_bottles.bottles_of_beer >= 2 <<*The error is pointing to
this line*
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
beer on the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"

else
puts "#{my.bottles.bottles_of_beer} #{my_bottles.bottle_word}of
beer on the wall"

end


if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
beer on the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

puts "No more #{my_bottles.bottle_word} of beer on the wall"

end


end
end




On Sun, Mar 1, 2009 at 1:24 AM, lasitha <(E-Mail Removed)> wrote:

> On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <(E-Mail Removed)> wrote:
> > I'm using Vim text editor, and have not yet figured out how to auto

> format my code.
>
> :h =
>
> In Normal mode, hitting == will format the current line.
> It takes the usual motion prefixes, so e.g. 5== will format the next 5
> lines.
> In Visual mode, = will format the selection.
>
> This should work for any recognized file type. Most standard vim
> installs include the ruby filetype plugin. If yours doesn't or needs
> upgrading, see:
> http://tinyurl.com/dd2ef9
>
> Solidarity,
> lasitha.
>
>


 
Reply With Quote
 
Justin Collins
Guest
Posts: n/a
 
      03-01-2009
Zayd Abdullah wrote:
> Thanks Lasitha that worked perfect. I'm slowly getting more comfortable with
> just the basic
> I'm still getting that same error when compiling my code. I know its right
> there in front of my face I just can't put my finger on it
>
> Kindest Regards
> Zayd
>
>
> class Bottles
> def initialize(bottles_of_beer, bottle_word, one_word)
> @bottles_of_beer = bottles_of_beer
> @bottle_word = bottle_word
> @one_word = one_word
>
> end
>



All the code past this point is in a very odd place. You seem to be
missing the rest of your Bottles class definition. Try adding another
'end' here.


>
> my_bottles = Bottles.new(99,'Bottles','Bottle')
>
>
> while my_bottles.bottles_of_beer >= 2


The error is right here because you never defined a bottles_of_beer
method for Bottles. In fact, you are still inside the Bottles class
definition, which I think you just forgot to close after the initialize
method.
You cannot access instance variables (@...) from outside of an object.
You have to define attribute setters and readers to provide the access.
Try using:

attr_accessor :bottles_of_beer
attr_reader :bottle_word, ne_word

These typically go right before your initialize method.You can read
about them here:
http://ruby-doc.org/docs/Programming...t_classes.html


> puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
> beer on the wall"
> puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
> beer"
> puts "Take one down, pass it around"
>
> my_bottles.bottles_of_beer -= 1
>
> if my_bottles.bottles_of_beer == 1
> puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"
>
> else
> puts "#{my.bottles.bottles_of_beer} #{my_bottles.bottle_word}of
> beer on the wall"
>


Typo above, should be my_bottles not my.bottles

> end
>
>
> if my_bottles.bottles_of_beer == 1
> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
> beer on the wall"
> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
> beer"
> puts "Take one down, pass it around"
>
> my_bottles.bottles_of_beer -= 1
>
> puts "No more #{my_bottles.bottle_word} of beer on the wall"
>
> end
>


None of this code should be in your class...end section.

-Justin

 
Reply With Quote
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-01-2009
[Note: parts of this message were removed to make it a legal post.]

Thanks Justin.
So the reason bottles_of_beer is attr_accessor is because I have to access
it to change the value of it, and bottle_word and one_word are attr_readers,
because those values never change?

Here goes my new code that worked just fine

class Bottles

attr_accessor :bottles_of_beer

attr_reader :bottle_word, ne_word

def initialize(bottles_of_beer, bottle_word, one_word)
@bottles_of_beer = bottles_of_beer
@bottle_word = bottle_word
@one_word = one_word

end

end


my_bottles = Bottles.new(99, 'Bottles', 'Bottle')

while my_bottles.bottles_of_beer >= 2
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer on
the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"

else
puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
beer on the wall"

end


if my_bottles.bottles_of_beer == 1
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer
on the wall"
puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer"
puts "Take one down, pass it around"

my_bottles.bottles_of_beer -= 1

puts "No more #{my_bottles.bottle_word} of beer on the wall"




end


end




On Sun, Mar 1, 2009 at 3:16 AM, Justin Collins <(E-Mail Removed)>wrote:

> Zayd Abdullah wrote:
>
>> Thanks Lasitha that worked perfect. I'm slowly getting more comfortable
>> with
>> just the basic
>> I'm still getting that same error when compiling my code. I know its right
>> there in front of my face I just can't put my finger on it
>>
>> Kindest Regards
>> Zayd
>>
>>
>> class Bottles
>> def initialize(bottles_of_beer, bottle_word, one_word)
>> @bottles_of_beer = bottles_of_beer
>> @bottle_word = bottle_word
>> @one_word = one_word
>>
>> end
>>
>>

>
>
> All the code past this point is in a very odd place. You seem to be missing
> the rest of your Bottles class definition. Try adding another 'end' here.
>
>
>
>> my_bottles = Bottles.new(99,'Bottles','Bottle')
>>
>>
>> while my_bottles.bottles_of_beer >= 2
>>

>
> The error is right here because you never defined a bottles_of_beer method
> for Bottles. In fact, you are still inside the Bottles class definition,
> which I think you just forgot to close after the initialize method.
> You cannot access instance variables (@...) from outside of an object. You
> have to define attribute setters and readers to provide the access.
> Try using:
>
> attr_accessor :bottles_of_beer
> attr_reader :bottle_word, ne_word
>
> These typically go right before your initialize method.You can read about
> them here: http://ruby-doc.org/docs/Programming...t_classes.html
>
>
> puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
>> beer on the wall"
>> puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of
>> beer"
>> puts "Take one down, pass it around"
>>
>> my_bottles.bottles_of_beer -= 1
>>
>> if my_bottles.bottles_of_beer == 1
>> puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"
>>
>> else
>> puts "#{my.bottles.bottles_of_beer} #{my_bottles.bottle_word}of
>> beer on the wall"
>>
>>

>
> Typo above, should be my_bottles not my.bottles
>
> end
>>
>>
>> if my_bottles.bottles_of_beer == 1
>> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
>> beer on the wall"
>> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of
>> beer"
>> puts "Take one down, pass it around"
>>
>> my_bottles.bottles_of_beer -= 1
>>
>> puts "No more #{my_bottles.bottle_word} of beer on the wall"
>>
>> end
>>
>>

>
> None of this code should be in your class...end section.
>
> -Justin
>
>


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-01-2009
Zayd Abdullah wrote:

> So the reason bottles_of_beer is attr_accessor is because I have to access
> it to change the value of it, and bottle_word and one_word are attr_readers,
> because those values never change?


Short term, use accessors to get to your instance variables.

Long term, learn the principle "tell don't ask." Don't ask your object for its
variables, then use them. Instead, tell the object what to do, and let it use
its variables to do it.

In your sample program, each phase (checking the beer bottle count, singing
about one bottles, decrementing the count) could have ran inside a method in
Bottles.

--
Phlip
 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      03-01-2009
Phlip wrote:
> Zayd Connor wrote:
>
>> I get the following error message when running the code below:
>> undefine method 'bottles _of_beer' for #<Bottles:0x2be1e1c>
>> (NoMethodError)

>
>> class Bottles
>> def initialize (bottles_of_beer, bottle_word, one_word)

> ^
>
> Ruby is space sensitive between the end of a method name and its first
> parameter.


Erm, what? No it's not. I just tried something like this in irb, and it
worked fine.

 
Reply With Quote
 
Gregory Brown
Guest
Posts: n/a
 
      03-01-2009
On Sun, Mar 1, 2009 at 1:24 AM, lasitha <(E-Mail Removed)> wrote:
> On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <(E-Mail Removed)> wrote:
>> I'm using Vim text editor, and have not yet figured out how to auto format my code.

>
> :h =
>
> In Normal mode, hitting == will format the current line.
> It takes the usual motion prefixes, so e.g. 5== will format the next 5 lines.
> In Visual mode, = will format the selection.


Thanks for posting this, even though I've been using vim forever, I
never bothered to look up how to do auto-formatting.

-greg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
What's the good book for ruby beginner and where can I find a ruby project? Guofu Chen Ruby 2 09-08-2007 01:41 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments