Ruby beginner Problem

Discussion in 'Ruby' started by Zayd Connor, Mar 1, 2009.

  1. Zayd Connor

    Zayd Connor Guest

    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/.
     
    Zayd Connor, Mar 1, 2009
    #1
    1. Advertising

  2. Zayd Connor

    Phlip Guest

    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
     
    Phlip, Mar 1, 2009
    #2
    1. Advertising

  3. [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 <> 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
    >
    >
     
    Zayd Abdullah, Mar 1, 2009
    #3
  4. Zayd Connor

    lasitha Guest

    On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <> 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.
     
    lasitha, Mar 1, 2009
    #4
  5. [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 <> wrote:

    > On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <> 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.
    >
    >
     
    Zayd Abdullah, Mar 1, 2009
    #5
  6. 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, :eek:ne_word

    These typically go right before your initialize method.You can read
    about them here:
    http://ruby-doc.org/docs/ProgrammingRuby/html/tut_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
     
    Justin Collins, Mar 1, 2009
    #6
  7. [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, :eek: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 <>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, :eek:ne_word
    >
    > These typically go right before your initialize method.You can read about
    > them here: http://ruby-doc.org/docs/ProgrammingRuby/html/tut_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
    >
    >
     
    Zayd Abdullah, Mar 1, 2009
    #7
  8. Zayd Connor

    Phlip Guest

    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
     
    Phlip, Mar 1, 2009
    #8
  9. 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.
     
    David Masover, Mar 1, 2009
    #9
  10. On Sun, Mar 1, 2009 at 1:24 AM, lasitha <> wrote:
    > On Sun, Mar 1, 2009 at 10:46 AM, Zayd Connor <> 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
     
    Gregory Brown, Mar 1, 2009
    #10
  11. [Note: parts of this message were removed to make it a legal post.]

    On Sun, Mar 1, 2009 at 1:39 PM, Phlip <> wrote:

    > 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
    >
    > Like this. I just created two methods in the Bottles class that sings the

    bottles song. I'm sure theres some cleaning up to do, Am I still doing to
    much outside of the class, can I add more to these methods?

    class Bottles

    attr_accessor :bottles_of_beer

    attr_reader :bottle_word, :eek: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

    def beersong
    puts "#{bottles_of_beer} #{bottle_word} of beer on the wall"
    puts "#{bottles_of_beer} #{bottle_word} of beer"
    puts "Take one down, pass it around"

    end

    def beersong_two
    puts "#{bottles_of_beer} #{one_word} of beer on the wall"
    puts "#{bottles_of_beer} #{one_word} of beer"
    puts "Take one down, pass it around"
    end
    end


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


    while my_bottles.bottles_of_beer >= 2

    my_bottles.beersong
    my_bottles.bottles_of_beer -=1

    if my_bottles.bottles_of_beer > 1
    puts "#{my_bottles.bottles_of_beer} bottles of beer on the wall"
    else
    puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"
    end

    if my_bottles.bottles_of_beer == 1

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

    end

    end
     
    Zayd Abdullah, Mar 1, 2009
    #11
  12. Mike Stephens, Mar 2, 2009
    #12
  13. [Note: parts of this message were removed to make it a legal post.]

    <<< I don't think you actually need a class at all You're just calling a
    procedure.

    Thanks, but I was just trying to practice using classes, and instance
    variables :)
    However I did find a quicker approach using less code to run this program
    without using class. See Below

    bottles = 99

    98.times do |num|
    puts "#{bottles} bottles of beer on the wall"
    puts "#{bottles} bottles of beer"
    puts "Take one down pass it around"
    bottles -=1
    puts "#{bottles} bottles of beer on the wall" unless bottles == 1

    if bottles == 1
    puts "#{bottles} bottle of beer on the wall"
    puts "#{bottles} bottle of beer on the wall"
    puts "#{bottles} bottle of beer"
    puts "Take one down pass it around"
    puts "No more bottles of beer on the wall"
    end
    end

    Kindest Regards
    Zayd

    On Mon, Mar 2, 2009 at 3:38 AM, Mike Stephens <> wrote:

    > I don't think you actually need a class at all You're just calling a
    > procedure.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Zayd Abdullah, Mar 2, 2009
    #13
  14. Zayd Connor

    Todd Benson Guest

    On Sun, Mar 1, 2009 at 12:23 PM, Zayd Abdullah <> wrote:
    > 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, :eek: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


    Just to explore the language a bit...


    module Grammar
    Noun = Struct.new( :singular, :plural, :collective )
    end
    include Grammar
    bottleness = Noun.new( "bottle", "bottles", "case" )
    10.downto(1) do |i|
    puts("#{i} #{i == 1 ? bottleness.singular : bottleness.plural} of
    beer on the wall")
    end


    ...of course you'd need to add the other phrases of the sing/song in
    there. Why a module? I guess it just seemed to make sense at the
    moment. Why a Struct? Because it builds accessors for me
    automatically.

    In reality, I'd try to separate the execution code from the string
    phrases (I pretty much try to avoid the #{} as much as I can).

    One thing interesting I should mention, though, is that the #{}
    construct can be used to not only display variable values, but also
    method results, and even expressions, so...

    99.downto(1) do |i|
    suffix = (i == 1 ? '' : 's')
    puts "#{i} bottle#{suffix} of beer on the wall,"
    puts "#{i} bottle#{suffix} of beer,"
    puts "Take one down pass it around,"
    puts "#{i-1} bottle#{'s' unless i == 2} of beer on the wall.\n"
    #that last line actually works
    puts
    end

    ...simple method example...

    def foo
    'hi'
    end

    puts "{#foo} there"


    ...but, like I said. Finding a way to separate static data from
    logical data is usually the best route for good code (which I have
    _not_ done in the above examples). And don't forget for that last
    edge case, namely, "No more" for zero.

    hth,
    Todd
     
    Todd Benson, Mar 2, 2009
    #14
  15. [Note: parts of this message were removed to make it a legal post.]

    Cool!, Wow, there are so many ways to do one thing. I'm going to try and
    test this out. I guess that is a good way to enhance ones skill, build
    simple programs, and try it in different ways.

    Kindest Regards

    On Mon, Mar 2, 2009 at 2:49 PM, Todd Benson <> wrote:

    > On Sun, Mar 1, 2009 at 12:23 PM, Zayd Abdullah <>
    > wrote:
    > > 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, :eek: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

    >
    > Just to explore the language a bit...
    >
    >
    > module Grammar
    > Noun = Struct.new( :singular, :plural, :collective )
    > end
    > include Grammar
    > bottleness = Noun.new( "bottle", "bottles", "case" )
    > 10.downto(1) do |i|
    > puts("#{i} #{i == 1 ? bottleness.singular : bottleness.plural} of
    > beer on the wall")
    > end
    >
    >
    > ...of course you'd need to add the other phrases of the sing/song in
    > there. Why a module? I guess it just seemed to make sense at the
    > moment. Why a Struct? Because it builds accessors for me
    > automatically.
    >
    > In reality, I'd try to separate the execution code from the string
    > phrases (I pretty much try to avoid the #{} as much as I can).
    >
    > One thing interesting I should mention, though, is that the #{}
    > construct can be used to not only display variable values, but also
    > method results, and even expressions, so...
    >
    > 99.downto(1) do |i|
    > suffix = (i == 1 ? '' : 's')
    > puts "#{i} bottle#{suffix} of beer on the wall,"
    > puts "#{i} bottle#{suffix} of beer,"
    > puts "Take one down pass it around,"
    > puts "#{i-1} bottle#{'s' unless i == 2} of beer on the wall.\n"
    > #that last line actually works
    > puts
    > end
    >
    > ...simple method example...
    >
    > def foo
    > 'hi'
    > end
    >
    > puts "{#foo} there"
    >
    >
    > ...but, like I said. Finding a way to separate static data from
    > logical data is usually the best route for good code (which I have
    > _not_ done in the above examples). And don't forget for that last
    > edge case, namely, "No more" for zero.
    >
    > hth,
    > Todd
    >
    >
     
    Zayd Abdullah, Mar 2, 2009
    #15
  16. Zayd Connor

    Todd Benson Guest

    > On Mon, Mar 2, 2009 at 2:49 PM, Todd Benson <> wrote:
    >> ...simple method example...
    >>
    >> def foo
    >> 'hi'
    >> end
    >>
    >> puts "{#foo} there"


    This obviously was supposed to read...

    puts "#{foo} there"
     
    Todd Benson, Mar 2, 2009
    #16
  17. Zayd Connor

    Todd Benson Guest

    On Mon, Mar 2, 2009 at 2:25 PM, Zayd Abdullah <> wrote:
    > Cool!, Wow, there are so many ways to do one thing.


    Yeah, I know. You should check out ruby quiz answers; so many good
    ways of doing the same quiz that changes weekly (if you haven't seen
    it). I am truly lowly compared to most of these people :)

    I tried my hand at the quiz several times, but was embarrassed with my
    code, lol! Good for learning though. Most of the quiz contributors
    are very good at commenting what they are doing.

    Todd
     
    Todd Benson, Mar 2, 2009
    #17
  18. [Note: parts of this message were removed to make it a legal post.]

    I think I'm going to try a few quiz's out, I never tried before, I just
    wanted to wait until I was comfortable with the basic foundations of Ruby.
    Thanks for your help, and suggestions =). Maybe I'lllook for the easy ones
    first, then take a look at the difficult ones.

    Kindest Regards
    Zayd

    On Mon, Mar 2, 2009 at 4:11 PM, Todd Benson <> wrote:

    > On Mon, Mar 2, 2009 at 2:25 PM, Zayd Abdullah <>
    > wrote:
    > > Cool!, Wow, there are so many ways to do one thing.

    >
    > Yeah, I know. You should check out ruby quiz answers; so many good
    > ways of doing the same quiz that changes weekly (if you haven't seen
    > it). I am truly lowly compared to most of these people :)
    >
    > I tried my hand at the quiz several times, but was embarrassed with my
    > code, lol! Good for learning though. Most of the quiz contributors
    > are very good at commenting what they are doing.
    >
    > Todd
    >
    >
     
    Zayd Abdullah, Mar 2, 2009
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    657
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    1,048
    Mabden
    Sep 2, 2004
  3. Guofu Chen
    Replies:
    2
    Views:
    141
    Morton Goldberg
    Sep 8, 2007
  4. Konrad SÅ‚abig

    Ruby/Sdl, beginner problem, making sprites...

    Konrad SÅ‚abig, Feb 17, 2009, in forum: Ruby
    Replies:
    0
    Views:
    115
    Konrad SÅ‚abig
    Feb 17, 2009
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    262
    7stud --
    Apr 12, 2011
Loading...

Share This Page