That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call Object from now on).
In Ruby, everything that can be manipulated by the program (i.e. every object) is also an Object, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren't Objects. In Ruby, this distinction doesn't exist: every object is an Object and every Object is also an object.
However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor Objects. These are, for example, methods, variables, syntax, parameter lists, argument lists, keywords.
Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
So, when we say "everything is an object", what we really mean is that "every object is an Object", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.
why do if.class and unless.class give no return value
Well, first off, even ififwere an object, those don't do what you think they do: when you say something like foo in Ruby, it means either "dereference the local variable foo" or "call the method foo with self as the implicit receiver and no argument list". So,
if.class
would either give you the class of the object referenced by the local variable if or the class of the object returned by the method if, but never the class of if itself.
But the if control flow keyword isn't an object, anyway (neither an object nor an Object) because keywords and control flow aren't objects in Ruby.
In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
every value is an object
Note, it doesn't say every-thing, only every value.
See also the question Is variable is object in ruby?
My advice would depend on your own goals, which might look like this... you might want to ask yourself (or score each of these from 1-10) if you prefer to:
learn a new language you might use in future? = Ruby
deepen your Python skills by using it for everything (say Django or Web.Py) = Python
move the Ruby testing away from Rails = Ruby
Other questions you could ask yourself to help the decision might be:
is speed important? Do some tests in the various languages. (If Ruby, then use Ruby 1.9 and get the other Ruby book).
is integration important? If so, why use a PHP front end?
is your connection to the language community important? If so, choose on 'community feel'.
is there a lot of backend text processing? (Perl?)
do you want to use an ORM or write SQL? = look at Ruby and Python lightweight frameworks.
I don't think the libraries will be an issue, since (I'm pretty sure that) libraries for popular languages cover all common tasks.
If you can score all the above from 1-10 it may help isolate a preferred direction...
Then, as I see it, the issue breaks down into 3 things:
what language do you most like to code in (work should be enjoyable)?
can the front end and back end be generated in a single language?
do you want to use a framework or a ready-made CMS for the front end?
It's worth looking at the origins of languages: PHP was originally announced as an extension of SSI, Ruby tries to take the best of Perl, Smalltalk and Lisp but has elements of a C/Java-like syntax, Perl is intimately connected to Unix and everywhere, although usually invisible to end users (despite some very good Perl web frameworks). You already know about Python.
As for frameworks and CMSs, a trawl through the distinctions/limitations/features might also help. It is too easy to install a PHP CMS (fine for a site with a well-defined purpose) but then find yourself hampered in acres of impenetrable code when you want do do something it can't do out of the box. A framework in the backend language will enable you to hook the back and front ends together more easily.
That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call Object from now on).
In Ruby, everything that can be manipulated by the program (i.e. every object) is also an Object, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren't Objects. In Ruby, this distinction doesn't exist: every object is an Object and every Object is also an object.
However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor Objects. These are, for example, methods, variables, syntax, parameter lists, arguments lists, keywords.
Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
So, when we say "everything is an object", what we really mean is that "every object is an Object", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.
so variables are objects
No, unfortunately, they are neither object s nor Objects.
This is also clearly stated in the Ruby Language Specification (emphasis added by me):
6.2 Variables
6.2.1 General description
A variable is denoted by a name, and refers to an object, which is called the value of the variable.
A variable itself is not an object.
In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
every value is an object
Note, it doesn't say every-thing, only every value.
See also the question Is variable is object in ruby?
One in particular that I would very much recommend is the free Ruby on Rails Tutorial by Michael Hartl. It's up to date and teaches you how to use not only Ruby and Rails, but also other modern and important tools such as RVM for Ruby/gem management, Git for version control and RSpec for testing. It encourages a great test-driven workflow, so I'd recommend it even if you're not a beginner. I actually used this tutorial a few months back to refresh my memory and get into the new Rails 3 after having been out of it for over a year.
After you're comfortable with the Rails framework, then you should learn the Ruby language it is built upon. This is the stage I am at. Some of the books I'd recommend for learning Ruby are:
Obviously this is based on my experience and recommendations I have received from friends and colleagues, so I'm not saying this is the path for everyone interested in Ruby/Rails, but so far it's working for me. I'll be interested in seeing what resources others recommend here.
class Example
@foo # class instance variable
@@bar # class variable
def fun1
@baz # instance variable
end
end
Instance variables
Instance variables (@foo and @baz in the example) always begin with @, and they always belong to whatever object self refers to: either an object of the class, or the Class object representing a class. An instance variable reference in a class definition or class method is completely different from an instance variable reference in an instance method.
Inheritance
Because instance variables are not defined by a class, they are unrelated to the inheritance mechanism —they are simply created when a value is assigned to them. Class instance variables, being simply instance variables of the Class object that represents a class, are thus not inherited.
Class variables
Class variables are visible to, and shared by, the class methods and the instance methods of a class, and also by the class definition itself. Class variables can be used in instance methods, class methods, and in the class definition itself, outside of any method. Class variables are always evaluated in reference to the class object created by the enclosing class definition statement.
Class instance variable vs instance variable
A disadvantage of class instance variables is that they cannot be used within instance methods as class variables can. Another disadvantage is the potential for confusing them with ordinary instance variables. An advantage of class instance variables over class variables has to do with the confusing behavior of class variables when subclassing an existing class: if a class uses class variables, then any subclass can alter the behavior of the class and all its descendants by changing the value of the shared class variable. This is a strong argument for the use of class instance variables instead of class variables.
I second the Rails Way book , its a great book thats easy to follow and organized. For learning Ruby I found
http://www.amazon.com/Ruby-Programming-Language-David-Flanag...
to be great
Rails is such a fast moving target that keeping totally updated books on the shelves is probably just as frustrating for the book publishers as for those wanting up-to-date books.
That said, the Pragmatic Programmer's book Agile Development with Rails is a pretty good intro: http://pragprog.com/titles/rails3/agile-web-development-with...
But, with Rails 3 (with many changes) coming in the next few months, books are going to become outdated yet again.
As for Ruby in general, the fairly recent The Ruby Programming Language (http://www.amazon.com/Ruby-Programming-Language-David-Flanag...) is a pretty good intro. David Black's The Well-Grounded Rubyist (http://www.amazon.com/Well-Grounded-Rubyist-David-Black/dp/1...) is recent, and I'm sure a great book, as his previous (Ruby for Rails) was excellent.
That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call
Object
from now on).In Ruby, everything that can be manipulated by the program (i.e. every object) is also an
Object
, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren'tObjects
. In Ruby, this distinction doesn't exist: every object is anObject
and everyObject
is also an object.However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor
Object
s. These are, for example, methods, variables, syntax, parameter lists, argument lists, keywords.Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
So, when we say "everything is an object", what we really mean is that "every object is an
Object
", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.Well, first off, even if
if
were an object, those don't do what you think they do: when you say something likefoo
in Ruby, it means either "dereference the local variablefoo
" or "call the methodfoo
withself
as the implicit receiver and no argument list". So,would either give you the class of the object referenced by the local variable
if
or the class of the object returned by the methodif
, but never the class ofif
itself.But the
if
control flow keyword isn't an object, anyway (neither an object nor anObject
) because keywords and control flow aren't objects in Ruby.In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
Note, it doesn't say every-thing, only every value.
See also the question Is variable is object in ruby?
This is the book from the master (aka Ruby creator) himself: http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177
You need this.
My advice would depend on your own goals, which might look like this... you might want to ask yourself (or score each of these from 1-10) if you prefer to:
Other questions you could ask yourself to help the decision might be:
I don't think the libraries will be an issue, since (I'm pretty sure that) libraries for popular languages cover all common tasks.
If you can score all the above from 1-10 it may help isolate a preferred direction...
Then, as I see it, the issue breaks down into 3 things:
It's worth looking at the origins of languages: PHP was originally announced as an extension of SSI, Ruby tries to take the best of Perl, Smalltalk and Lisp but has elements of a C/Java-like syntax, Perl is intimately connected to Unix and everywhere, although usually invisible to end users (despite some very good Perl web frameworks). You already know about Python.
As for frameworks and CMSs, a trawl through the distinctions/limitations/features might also help. It is too easy to install a PHP CMS (fine for a site with a well-defined purpose) but then find yourself hampered in acres of impenetrable code when you want do do something it can't do out of the box. A framework in the backend language will enable you to hook the back and front ends together more easily.
That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call
Object
from now on).In Ruby, everything that can be manipulated by the program (i.e. every object) is also an
Object
, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren'tObjects
. In Ruby, this distinction doesn't exist: every object is anObject
and everyObject
is also an object.However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor
Object
s. These are, for example, methods, variables, syntax, parameter lists, arguments lists, keywords.Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
So, when we say "everything is an object", what we really mean is that "every object is an
Object
", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.No, unfortunately, they are neither object s nor
Object
s.This is also clearly stated in the Ruby Language Specification (emphasis added by me):
In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
Note, it doesn't say every-thing, only every value.
See also the question Is variable is object in ruby?
I took up Rails with the Agile Web Development with Rails 2nd Edition. I have their latest (4th) edition and recommend it, but there are lots of other resources out there now.
One in particular that I would very much recommend is the free Ruby on Rails Tutorial by Michael Hartl. It's up to date and teaches you how to use not only Ruby and Rails, but also other modern and important tools such as RVM for Ruby/gem management, Git for version control and RSpec for testing. It encourages a great test-driven workflow, so I'd recommend it even if you're not a beginner. I actually used this tutorial a few months back to refresh my memory and get into the new Rails 3 after having been out of it for over a year.
After you're comfortable with the Rails framework, then you should learn the Ruby language it is built upon. This is the stage I am at. Some of the books I'd recommend for learning Ruby are:
Obviously this is based on my experience and recommendations I have received from friends and colleagues, so I'm not saying this is the path for everyone interested in Ruby/Rails, but so far it's working for me. I'll be interested in seeing what resources others recommend here.
I would use pure ruby (Matz Ruby Interpreter (MRI)) to start off. My understanding is that iron ruby is not quite ready yet.
If you are looking for a good book my current favorite (over pickaxe) is http://www.amazon.com/gp/product/0596516177 by matz and flanagan, the book is very concise well written paragraphs and they provide great examples (in 1.8.* and 1.9)
Enjoy! :D
There is a method to this madness...
Instance variables
Instance variables (
@foo
and@baz
in the example) always begin with@
, and they always belong to whatever object self refers to: either an object of the class, or the Class object representing a class. An instance variable reference in a class definition or class method is completely different from an instance variable reference in an instance method.Inheritance
Because instance variables are not defined by a class, they are unrelated to the inheritance mechanism —they are simply created when a value is assigned to them. Class instance variables, being simply instance variables of the Class object that represents a class, are thus not inherited.
Class variables
Class variables are visible to, and shared by, the class methods and the instance methods of a class, and also by the class definition itself. Class variables can be used in instance methods, class methods, and in the class definition itself, outside of any method. Class variables are always evaluated in reference to the class object created by the enclosing class definition statement.
Class instance variable vs instance variable
A disadvantage of class instance variables is that they cannot be used within instance methods as class variables can. Another disadvantage is the potential for confusing them with ordinary instance variables. An advantage of class instance variables over class variables has to do with the confusing behavior of class variables when subclassing an existing class: if a class uses class variables, then any subclass can alter the behavior of the class and all its descendants by changing the value of the shared class variable. This is a strong argument for the use of class instance variables instead of class variables.
Much of this is from the excellent "The Ruby Programming Language"
alt text http://ecx.images-amazon.com/images/I/517LDwIEYwL._SL75_.jpg
That said, the Pragmatic Programmer's book Agile Development with Rails is a pretty good intro: http://pragprog.com/titles/rails3/agile-web-development-with...
But, with Rails 3 (with many changes) coming in the next few months, books are going to become outdated yet again.
As for Ruby in general, the fairly recent The Ruby Programming Language (http://www.amazon.com/Ruby-Programming-Language-David-Flanag...) is a pretty good intro. David Black's The Well-Grounded Rubyist (http://www.amazon.com/Well-Grounded-Rubyist-David-Black/dp/1...) is recent, and I'm sure a great book, as his previous (Ruby for Rails) was excellent.