Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Ruby beginner Problem

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

On Sun, Mar 1, 2009 at 1:39 PM, Phlip <(E-Mail Removed)> 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, 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

 
Reply With Quote
 
 
 
 
Mike Stephens
Guest
Posts: n/a
 
      03-02-2009
I don't think you actually need a class at all You're just calling a
procedure.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-02-2009
[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 <(E-Mail Removed)> 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/.
>
>


 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-02-2009
On Sun, Mar 1, 2009 at 12:23 PM, Zayd Abdullah <(E-Mail Removed)> 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, 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, lural, :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

 
Reply With Quote
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-02-2009
[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 <(E-Mail Removed)> wrote:

> On Sun, Mar 1, 2009 at 12:23 PM, Zayd Abdullah <(E-Mail Removed)>
> 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, 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, lural, :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
>
>


 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-02-2009
> On Mon, Mar 2, 2009 at 2:49 PM, Todd Benson <(E-Mail Removed)> wrote:
>> ...simple method example...
>>
>> def foo
>> 'hi'
>> end
>>
>> puts "{#foo} there"


This obviously was supposed to read...

puts "#{foo} there"

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-02-2009
On Mon, Mar 2, 2009 at 2:25 PM, Zayd Abdullah <(E-Mail Removed)> 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

 
Reply With Quote
 
Zayd Abdullah
Guest
Posts: n/a
 
      03-02-2009
[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 <(E-Mail Removed)> wrote:

> On Mon, Mar 2, 2009 at 2:25 PM, Zayd Abdullah <(E-Mail Removed)>
> 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
>
>


 
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