If local_variable == @variable
-
My SU page fonts are huge today,-), my eyes are not that bad, yet. Anyway I notice that I can not test different kinds of variables, as the following fails:
def anything something if local_variable == @variable something else something else end endWhile the following works:
def anything something if @local_variable == @variable something else something else end endIs it a rule in Ruby that you can not test different kinds of variables? Is there other things I need to be aware of because of this?
-
@honoluludesktop said:
My SU page fonts are huge today,-)
SCF you mean? At the top of the page, upper right, you can swap between sizes.
@honoluludesktop said:
Anyway I notice that I can not test different kinds of variables, as the following fails:
Fails with error message, or just doesn't do what you want?
There should be no restrictions, but are you sure you defined all the variables?
-
Yeah, at the office, I get to adjust the view zoom font %, so I found that I had to scale down from 125% to 100%. Don't know why the machine woke up at 125%.
Don't recall error massage, but I didn't separately declare the local variable (vari_1 = 0, vari_2 = 0). OK, I thought that local variables could be created on the fly. Sigh...
-
@honoluludesktop said:
variables could be created on the fly. Sigh...
` x=3
3
puts 'hello' if y == x
Error: #<NameError: undefined local variable or methody' for main:Object> (eval):15when comparing you need to have initialized the variable.
-
Note that instance variables doesn't act the same:
puts 'hello' if @y == x nil -
OK, thanks.
-
@honoluludesktop said:
My SU page fonts are huge today,-), my eyes are not that bad, yet. Anyway I notice that I can not test different kinds of variables, as the following fails:
def anything > something > if local_variable == @variable > something > else > something else > end > endWhile the following works:
def anything > something > if @local_variable == @variable > something > else > something else > end > endIs it a rule in Ruby that you can not test different kinds of variables? Is there other things I need to be aware of because of this?
you need to initialize the local variables. Otherwise you get an error.
Instance variables (with
@) are initialized tonilby default, and this is why you don't get an error.Fredo
-
And class variables
@@.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement