[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Affecting variables inside a while loop that are initialised outside its scope

Rawn

11/6/2007 4:02:00 AM

How can I do it? Sorry if this is stupid or something but I'm just
learning to program (starting with ruby) and this seemed like a more
interesting way to start than endless arithmatic operations..

#!/usr/bin/ruby
Inventory = Array.new
Discovered = Array.new
Victory = 0
puts "You are in a dim room. You can just make out a door by the light
shining through the cracks."
while Victory < 1
inputcommand = gets.chomp!
if inputcommand = "search"
Victory + 1 == Victory
end
end


I'd like to change the value of Victory to 1 when "search" is typed
into the prompt, thus causing the while loop to end, ending the
script. This may be the completly wrong way to go about doing this but
atleast tell me how to do what I want before you school me in the
proper way of doing things ; )
thanks in advance

5 Answers

mortee

11/6/2007 5:10:00 AM

0

Rawn wrote:
> How can I do it? Sorry if this is stupid or something but I'm just
> learning to program (starting with ruby) and this seemed like a more
> interesting way to start than endless arithmatic operations..
>
> #!/usr/bin/ruby
> Inventory = Array.new
> Discovered = Array.new
> Victory = 0
> puts "You are in a dim room. You can just make out a door by the light
> shining through the cracks."
> while Victory < 1
> inputcommand = gets.chomp!
> if inputcommand = "search"
> Victory + 1 == Victory
> end
> end
>
>
> I'd like to change the value of Victory to 1 when "search" is typed
> into the prompt, thus causing the while loop to end, ending the
> script. This may be the completly wrong way to go about doing this but
> atleast tell me how to do what I want before you school me in the
> proper way of doing things ; )
> thanks in advance

Yes, you're doing it wrong (:

If you want to set a variable to 1, simply set it so:

Victory = 1

If you want to increment it each time some condition is met, then set it
to a bigger value:

Victory = Victory + 1

or more simply

Victory += 1

Also let me point out that you seem to completely swap the meaning of
assignment and testing. When you verify for equality, you use the ==
operator, and when assigning, you use =, not the other way around. So if
you want to check if you just read a specific string from the console,
you do this:

if inputcommand == "search"

otherwise inputcommand will be *set to* the value "search", and the
whole expression's value checked by "if", which will always evaluate to
true.

Note that values whose name starts with a capital letter are constants,
or class or module names (which also happen to be constants). While you
*can* in fact alter the value of a constant in Ruby, you shouldn't use
them as variables (e.g. you get a warning when you modify them, and also
you can't create them inside methods, AFAIK). So you should just use
variable names starting with lowercase letters or underscore.

Further, you can simply use boolean values when you use a variable as a
boolean flag, you don't have to compare integers.

victory = false
...
until victory
...
victory = true
end

Also, in such a simple loop you can avoid using a condition variable
altogether, and simply break out the loop when you want to:

while true
...
break # immediately finish the loop when passing this line
end

I guess most of what I've described can be found in any entry level
guides to Ruby. You really should take your time to read and understand
one - go look around www.ruby-lang.org for documentation.

mortee


7stud --

11/6/2007 5:12:00 AM

0

You have = and == mixed up. == compares two values. Also, to add 1 to
a variable you do the adding on the right side of an = sign:

val = 0
val = val + 1
#or the shorthand:
val += 1

--
Posted via http://www.ruby-....

Harry Kakueki

11/6/2007 5:18:00 AM

0

On 11/6/07, Rawn <glasyalabolis@gmail.com> wrote:
>
> I'd like to change the value of Victory to 1 when "search" is typed
> into the prompt, thus causing the while loop to end, ending the
> script. This may be the completly wrong way to go about doing this but
> atleast tell me how to do what I want before you school me in the
> proper way of doing things ; )
> thanks in advance
>
>
>

# Not tested but first try these modifications.

inventory = Array.new
discovered = Array.new
victory = 0 # small v. Ruby sees Victory as constant.
puts "You are in a dim room. You can just make out a door by the light
shining through the cracks."
while victory < 1
inputcommand = gets.chomp!
if inputcommand == "search" # use == not =
victory += 1 # or victory = victory + 1
end
end

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Arlen Cuss

11/6/2007 5:19:00 AM

0

Hi,

On Tue, 2007-11-06 at 13:05 +0900, Rawn wrote:
> How can I do it? Sorry if this is stupid or something but I'm just
> learning to program (starting with ruby) and this seemed like a more
> interesting way to start than endless arithmatic operations..
>
> #!/usr/bin/ruby
> Inventory = Array.new
> Discovered = Array.new
> Victory = 0
> puts "You are in a dim room. You can just make out a door by the light
> shining through the cracks."
> while Victory < 1
> inputcommand = gets.chomp!
> if inputcommand = "search"
> Victory + 1 == Victory
> end
> end
>
>
> I'd like to change the value of Victory to 1 when "search" is typed
> into the prompt, thus causing the while loop to end, ending the
> script. This may be the completly wrong way to go about doing this but
> atleast tell me how to do what I want before you school me in the
> proper way of doing things ; )
> thanks in advance

Firstly, something to become used to is that in Ruby, anything beginning
with a capital letter is a constant! Watch:

irb(main):001:0> Victory = 1
=> 1
irb(main):002:0> Victory = 2
(irb):2: warning: already initialized constant Victory
=> 2
irb(main):003:0>

This basically means you should leave capital letters at the start of
names for values which don't change -- for example, there's this
constant:

irb(main):003:0> RUBY_VERSION
=> "1.8.6"
irb(main):004:0>

That's built-in to Ruby, and of course it's not changing, so it's
rightfully a constant. So, your variable may need to be called 'victory'
instead.

Secondly, to assign a new value to 'victory', just do it like this:

irb(main):002:0> victory = 0
=> 0
irb(main):003:0> victory = victory + 1
=> 1
irb(main):004:0> victory = victory + 1
=> 2
irb(main):005:0>

Notice the way `victory' is evaluated on the right-hand side first, then
it's all assigned to `victory' on the left. There's even a shorthand for
adding to a variable:

irb(main):005:0> victory += 1
=> 3
irb(main):006:0> victory += 1
=> 4
irb(main):007:0>

You'll learn a lot more about this as you advance in your Ruby.

Finally, I'll let you know that "==" is for testing equality, whereas
"=" is always for assigning:

irb(main):010:0> victory == 3
=> false
irb(main):011:0> victory == 4
=> true
irb(main):012:0> victory == 5
=> false
irb(main):013:0>

So, == doesn't change the variable - it just returns 'true' or 'false'
like some other comparison operators do:

irb(main):013:0> victory < 5
=> true
irb(main):014:0> victory <= 2
=> false
irb(main):015:0>

These are what you use with 'if' or 'while' and similar constructs.
(this is why you need to take care when writing them not to forget an
extra '=' sign!)

Good luck!

Arlen


Rawn

11/6/2007 12:01:00 PM

0

Thanks for all the advice! I was kind of writing this as I went
through the guides and really couldn't find a way to make this work. I
guess I should have finished all of the guides : )
Thanks again for all of your kind advice