I’ve started learning Ruby and I’m really, really enjoying it. I have no
prior experience with any programming language, and I’ve been reading,
listening and watching as much Ruby related material I can.
Can someone please take a look at my code, it’s s simple loop as per
Chris P.'s Learn to Program book.
This is what I’ve come up with to print the 99 bottles poem
01| bottles = 99
02|
03| while bottles != 0
04| puts bottles.to_s + ’ of beer on the wall! ’ + bottles.to_s + ’ of
beer!’
05| puts ‘Take one down, and pass it around, now there is…’
06|
07| bottles = bottles - 1
08|
09| if bottles == 0
10| puts bottles.to_s + ’ of beer on the wall’
11| puts bottles.to_s + ’ bottles of beer!’
12| end
13|
14| end
Can someone tell me if I’m on the right track?
For example.
09| if bottles == 0
I’ve seen people use the following instead
Ex| if bottles != 1
What’s the the most correct context because I’m still wrapping my head
around this.
If != means “Are these different”
Example if I change the code from == to != it will do the same thing
09| if bottles != 1
10| puts bottles.to_s + ’ of beer on the wall’
11| puts bottles.to_s + ’ bottles of beer!’
12| end
So, will it count from 99, 98, 97, etc… when it reaches number 1 the !=
method will return a ‘false’ and therefor output whatever is in the if
statement, is this correct?
but if i read this in my head:
On Jul 17, 2008, at 3:15 AM, Devious Forlan wrote:
I’ve started learning Ruby and I’m really, really enjoying it. I
have no
prior experience with any programming language, and I’ve been reading,
listening and watching as much Ruby related material I can.
Great. Welcome to Ruby!
Can someone please take a look at my code, it’s s simple loop as per
Chris P.'s Learn to Program book.
Sure, I can take a look.
06|
07| bottles = bottles - 1
08|
09| if bottles == 0
10| puts bottles.to_s + ’ of beer on the wall’
11| puts bottles.to_s + ’ bottles of beer!’
12| end
13|
14| end
Looking good to me.
What’s the the most correct context because I’m still wrapping my head
around this.
As you said, the two are equivalent. There’s no right or wrong way to
represent it. It’s probably best to use whichever one you feel makes
the code the most clear. However, I don’t think there’s much
difference for this particular case.
Like you my brain can only comprehend simple things
I’m really hoping I can finish Chris P.'s Learn to Program tutorial
and take it from there.
Cheers
Marc H. wrote:
BTW instead of
bottles = bottles - 1
you can also do
bottles -= 1
Granted, it is not a huge change.
About
if bottles == 0
vs
if bottles != 1
I’d prefer the first. For some reason my brain likes simple things
like == more than != which becomes especially complicated for my
brain to process when it is
unless bottles != 1 and bottles >= 5
I’d prefer the first. For some reason my brain likes simple things
like == more than != which becomes especially complicated for my
brain to process when it is
unless bottles != 1 and bottles >= 5
Everyone’s brain likes simple things. Unfortunately one of the
side-effects of education is to make people believe that complex things
are somehow better. In kindergarten we learn that “Simple Simon” means
stupid Simon, and the process carries on right up to higher degree
level. Hence all those mathematical papers full of incomprehensible
formulae, most of which say nothing of interest.
I’ve come to the conclusion that 90% of people are “complicators”: give
them a problem and they’ll add bells and whistles to it and make it more
complex than it started out.
I like to think I’m in the other 10%, the “simplifiers”. We strip the
problem down to its bare necessities, see the underlying simplicity, and
get things done.
Dave
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.