SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 48
-
Jan 31, 2007, 07:55 #1
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Learn Ruby on Rails: the Ultimate Beginner's Guide
Notice: This is a discussion thread for comments about the SitePoint article, Learn Ruby on Rails: the Ultimate Beginner's Guide.
__________
I hope you haven't printed too many of these ;-).
& I'm sorry if this is not correct; I'm too lazy to test it ;-).
The example code contains an error.
Code:class Car @mileage = 0 # instance variable ... end
-
Feb 1, 2007, 12:39 #2
i don't understand a single word you said. In the book (in the ruby tutorial initial part) it doesn't speak about your "instance instance variable". What is that? It is just a mess.
-
Feb 1, 2007, 13:57 #3
Think of a it as a variable that's available to all members of the same class.
This excerpt from the Programming Ruby book explains it pretty well: http://www.rubycentral.com/book/tut_classes.html#S3
-
Feb 1, 2007, 18:03 #4
i only see (as in the sitepoint ruby book) class variables, class methods, instance variables and instance methods
-
Feb 2, 2007, 02:13 #5
Yes, what Fenrir was trying to explain is that a class is also an object (of type Class) and as such a class can also have instance variables:
Code:class Foo @bar = 'hello' # this is an instance variable of the Foo class itself (which is also an object) def initialize @bar = 'hello' # this is an instance variable for all instances of the Foo class end end
Code:# this: class Foo end # is the same as this: Foo = Class.new
-
Feb 2, 2007, 12:14 #6
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes it is quite simple actually once you understand it ;-). The difference between instance variables and class variables is in the scoping rules (where you can access the variable). If you set an instance variable of a particular object you can only access it from inside the object:
Code:class Foo def set_the_variable(value) @the_variable = value end def print_the_variable print @the_variable end end
Code:foo1 = Foo.new foo1.set_the_variable("foo1's variable") foo2 = Foo.new foo2.set_the_variable("foo2's variable") foo1.print_the_variable # "foo1's variable" foo2.print_the_variable # "foo2's variable"
A class variable is different: there is only one variable for all objects:
Code:class Foo def set_the_variable(value) @@the_variable = value end def print_the_variable print @@the_variable end end foo1 = Foo.new foo1.set_the_variable("foo1's variable") foo2 = Foo.new foo2.set_the_variable("foo2's variable") # this one overwrites foo1's variable too foo1.print_the_variable # "foo2's variable" !!! foo2.print_the_variable # "foo2's variable"
Code:class Foo # ... same as before ... def self.print_the_variable_from_the_class print @@the_variable end end foo1 = Foo.new foo1.set_the_variable("Hi!") Foo.print_the_variable_from_the_class # "Hi!"
Because classes are objects they can have instance variables too. You cannot access these variables in the instances of the class (foo1 and foo2) -- only from the class itself.
Code:class Bar @the_variable = "Hi!" def print_the_variable # note: this is an instance method print @the_variable # this won't work because this instance variable doesn't exist: it belongs to the class end def self.print_the_variable # note: this is a class method print @the_variable # this will work end end
-
Feb 2, 2007, 12:16 #7
Nice writeup fenrir
-
Feb 3, 2007, 06:55 #8
ok now i hope it is more clear
....let me do some explanation and tell me if i'm wrong:
There are 3 types of variables in a class:
1) Instances instance variable (and they are prefixed with a @ and ONLY declared in an instance method and can be accessed only from a particular instance of a class at a time);
2) Class variables (they are prefixed with a @@ and declared in instance methods and class methods and are available for all the instances of the class);
3) class instance variables ( they are prefixed with a @ and only declared outside any methods of that class).
-
Feb 3, 2007, 11:56 #9
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Correct
From Ruby's point of view types 1 and 3 are the same: they're just instance variables. Ruby makes no distinction between classes and objects (classes are objects) in this respect.
-
Feb 3, 2007, 14:14 #10
-
Feb 3, 2007, 16:23 #11
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No you cannot. (or: I don't know how). Most instance variables are defined in the constructor, which is an instance method.
Edit: well, you can do this with instance_variable_set:
Code:irb(main):001:0> class Foo irb(main):002:1> end => nil irb(main):003:0> f = Foo.new => #<Foo:0x282da3c> irb(main):004:0> f.instance_variable_set('@x', 4) => 4 irb(main):005:0> f.instance_variable_get('@x') => 4
-
Feb 4, 2007, 05:02 #12
ok, and class variables in a class outside any method?
-
Feb 4, 2007, 10:24 #13
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can access class variables in the class declaration, class methods AND in the instance methods.
-
Feb 5, 2007, 05:38 #14
so the error declared at the beggining of the post is real? Is it really an error or bad practise to initialize instance variables outside instance methods? If it is than the author of the book has made a very big mistake.....i hope typo mistake
-
Feb 6, 2007, 12:30 #15
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well the code still works but it creates a separate variable.
Code:class Foo @something = 4 # this variable def somemethod() @something = 3 # is totally distinct from this variable end end # much like something = 4 # this variable def yyy() something = 3 # isn't the same variable as this one, they just have the same name. end # so if we call yyy() # something still contains 4
If you do this:
Code:class Foo @something = 4 # this line sets the class instance variable @something def get_something return @something # this returns the instance instance variable @something end end a_foo = Foo.new a_foo.get_something # returns nil (the NULL value of Ruby) instead of 4, as most people would expect after reading the code sample of the book
-
Feb 6, 2007, 15:43 #16
perfectly clear
so the book example is valid anyway 'cause it is only a variable initialization to the value 0......and the instance instance variable in the instance method is assigned a value taken from the method parameter so it is irrilevant the real class instance variable initialization :P
is it right?
-
Feb 7, 2007, 11:24 #17
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's right
.
-
Feb 7, 2007, 20:54 #18
- Join Date
- Oct 2005
- Location
- Melbourne, Australia
- Posts
- 574
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Good spot Fenrir2, and an interesting discussion.
We made a decision not to include a discussion about class-level and object-level instance variables in an introductory chapter, for the sake of simplicity (and considering that class-level instance variables are rarely required when working with Rails).
However you are correct in that instance variables in Ruby do not need declaring, so the line initializing @mileage to 0, while not strictly incorrect, is indeed redundant and misleading. I've updated the article and the book's errata page to hopefully clarify this.Last edited by mattymcg; Feb 7, 2007 at 22:44. Reason: Corrected misspelling of Fenrir2's name!
I design beautiful, usable interfaces. Oh, and I wrote a kids' book.
• Follow me on Twitter.
• Read my blog.
• Buy my book, Charlie Weatherburn and the Flying Machine.
-
Feb 8, 2007, 02:25 #19
Ok but remember that your instance variable initalization(that not need to be initializated) is in fact a class instance initialization so you have also to move that initialization inside the instance method to do that a correct "instance variable initalization". right?
-
Feb 8, 2007, 02:27 #20
....then i found in the book sometimes code statements that are not correctcly explained. i.e. i find a name followed by a hash. What is it? a method with a parameter without parenthesis or what?
for example...i found this:
redirect_to :action => 'index'
it doesn't say anything about that "redirect_to" is a method and if the part that begginings with ":action..." is the paramether to the method without parenthesis.....or it is just a typo error?Last edited by Skyblaze; Feb 8, 2007 at 04:10.
-
Feb 8, 2007, 05:15 #21
- Join Date
- Oct 2005
- Location
- Melbourne, Australia
- Posts
- 574
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
@Skyblaze:
1) Yes, if you want to move @mileage = 0 into the initialize method you could do that, and that would correctly give the object-level instance variable an initial value of zero. However each time this variable is referenced in the chapter, it is initialized with a value first, so for the errata page I chose to remove its initialization altogether.
2) Re: "A name followed by a hash", could you be more specific? In Ruby, a hash (#) is used to indicate a comment -- this is explained on page 60.
3) The redirect_to method redirects the user to the controller action that you pass to it in the form of a symbol. The syntax redirect_to :action => 'my_action' and redirect_to(:action => 'my_action') are identical. The => operator assigns a value to an argument that is passed to a method (in Ruby, parentheses are often optional, as described in Chapter 3). Use of this operator in this manner is introduced in Chapter 5 (page 129).
Hope that helps.
MattLast edited by mattymcg; Feb 8, 2007 at 16:50.
I design beautiful, usable interfaces. Oh, and I wrote a kids' book.
• Follow me on Twitter.
• Read my blog.
• Buy my book, Charlie Weatherburn and the Flying Machine.
-
Feb 8, 2007, 07:04 #22
-
Feb 8, 2007, 17:03 #23
- Join Date
- Oct 2005
- Location
- Melbourne, Australia
- Posts
- 574
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorry to hear that - other customers have said the opposite, but of course everyone's different and thanks for letting us know. I'll definitely pass your feedback on to the author; we'll try to address some of the areas that you found confusing in future editions. And hopefully as you progress through the rest of the book you'll find value in what it teaches...
I design beautiful, usable interfaces. Oh, and I wrote a kids' book.
• Follow me on Twitter.
• Read my blog.
• Buy my book, Charlie Weatherburn and the Flying Machine.
-
Feb 9, 2007, 10:13 #24
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is a verbose version:
redirect_to({:action => 'index'});
We don't need parentheses and semicolons in Ruby:
redirect_to {:action => 'index'}
If you pass a hash in the last parameter you don't have to write the accolades:
redirect_to :action => 'index'
-
Feb 10, 2007, 04:12 #25
Bookmarks