Is everything an object in ruby?
Depends on what you mean by "everything". Fixnum
s are, as the others have demonstrated. Classes also are, as instances of class Class
. Methods, operators and blocks aren't, but can be wrapped by objects (Proc
). Simple assignment is not, and can't. Statements like while
also aren't and can't. Comments obviously also fall in the latter group.
Most things that actually matter, i.e. that you would wish to manipulate, are objects (or can be wrapped in objects).
Is everything an object in Python like Ruby?
DiveIntoPython - Everything Is an Object
Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute
__doc__
, which returns thedoc string
defined in the function's source code. Thesys
module is an object which has (among other things) an attribute calledpath
. And so forth.Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).
Ruby Docs - To Ruby From Python
As with Python, in Ruby,... Everything is an object
So there you have it from Ruby's own website: in Python everything is an object.
Java and Ruby: everything is an object in OO?
No.
As far as values go, the "primitive types" (int
, bool
, float
, etc.) in Java are not objects. In Ruby they are objects. (In some Ruby implementations fixnums are "value types" internally for performance, but externally they are treatable as "real" objects that have methods).
In addition, there are other things that are objects in Ruby that are not objects in Java such as classes. (Actually, Java exposes these as Class
as well, but in a different sense.)
There are some things that are not objects in either language, such as variables and methods. (Although in Ruby it is easy to get an object that represents a given method.)
Anyway, I think the bigger picture is that the Object Oriented programming paradigm presents a way to group data and operations on said data. (This is generally done through instance methods, as in Java and Ruby, although it might also be done through multiple dispatch and other languages, like Haskell which is "non-OO", offer alternative approaches to this task.)
Often times the definition of "OO" also includes "inheritance", "encapsulation", "abstraction", and other silly textbook terms, but the usage and patterns of different "OO" languages can vary greatly and overlap those found in "non-OO" languages ;-)
Why is EVERYTHING an instance of Class in Ruby?
First, is EVERYTHING in Ruby an instance of
Class
?
No, not everything is an instance of Class
. Only classes are instances of Class
.
There are lots of things which aren't instances of Class
: strings, for example, are instances of String
, not Class
. Arrays are instances of Array
, integers are instances of Integer
, floats are instances of Float
, true
is an instance of TrueClass
, false
is an instance of FalseClass
, nil
is an instance of NilClass
, and so on.
Every class is an instance of Class
, just like every string is an instance of String
.
if
Object
is a superclass ofClass
, how can it be both a superclass ofClass
and an instance of it at the same time (most diagrams on the Ruby Object Model clearly state this hierarchy)?
Magic.
Just like in most other languages, there are some core entities that are simply assumed to exist. They fall from the sky, materialize out of thin air, magically appear.
In Ruby, some of those magic things are:
Object
doesn't have a superclass, but you cannot define a class with no superclass, the implicit direct superclass is alwaysObject
. [Note: there may be implementation-defined superclasses ofObject
, but eventually, there will be one which doesn't have a superclass.]Object
is an instance ofClass
, which is a subclass ofObject
(which means that indirectlyObject
is an instance ofObject
itself)Class
is a subclass ofModule
, which is an instance ofClass
Class
is an instance ofClass
None of these things can be explained in Ruby.
BasicObject
, Object
, Module
and Class
all need to spring into existence at the same time because they have circular dependencies.
Just because this relationship cannot be expressed in Ruby code, doesn't mean the Ruby Language Specification can't say it has to be so. It's up to the implementor to figure out a way to do this. After all, the Ruby implementation has a level of access to the objects that you as a programmer don't have.
For example, the Ruby implementation could first create BasicObject
, setting both its superclass
pointer and its class
pointer to null
.
Then, it creates Object
, setting its superclass
pointer to BasicObject
and its class
pointer to null
.
Next, it creates Module
, setting its superclass
pointer to Object
and its class
pointer to null
.
Lastly, it creates Class
, setting its superclass
pointer to Module
and its class
pointer to null
.
Now, we can overwrite BasicObject
's, Object
's, Module
's, and Class
's class
pointer to point to Class
, and we're done.
This is easy to do from outside the system, it just looks weird from the inside.
Is it good that everything is an object ?
A counterexample would be that in Java Integer
is an object but int
is not, which means different operations apply to both (admittedly in recent Java there is automatic conversion to/from the object version, but this can introduce unexpected performance issues). Objects are a little slower due to indirection, but more flexible; everything being an object means everything behaves consistently. Again Java would be an example: an array is not an object, and ArrayIterator
is something that is bolted on after the fact (with multiple third party implementations, even) and therefore not quite consistent with the way collection class iterators work.
Ruby: Is variable is object in ruby?
"In ruby, everything is an object" is basically true.
But more accurately, I would say that any value that can be assigned to a variable or returned from a method is an object. Is a variable an object? Not really. A variable is simply a name of an object (also known as a "pointer") that allows you locate it in memory and do stuff with it.
shajin = Person.new()
In this snippet, we have a variable shajin
, which points to an object (an instance of the person class). The variable is simply the identifier for an object, but is not the object itself.
I think it was a trick question. Ultimately object orientation is feature for humans to understand complex programs, but computers are not object oriented themselves. Drill down enough layers and objects cease to exist in any language.
So perhaps it's more fair to say: "In ruby, everything important is an object".
Determining type of an object in ruby
The proper way to determine the "type" of an object, which is a wobbly term in the Ruby world, is to call object.class
.
Since classes can inherit from other classes, if you want to determine if an object is "of a particular type" you might call object.is_a?(ClassName)
to see if object
is of type ClassName
or derived from it.
Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called "Duck typing". In other words, if it responds to the methods you want, there's no reason to be particular about the type.
For example, object.is_a?(String)
is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. object.respond_to?(:to_s)
would be a better way to test that the object in question does what you want.
Examples of 'Things' that are not Objects in Ruby
The most obvious one that jumps into my head would be blocks. Blocks can be easily reified to a Proc
object, either by using the &block
parameter form in a parameter list or by using lambda
, proc
, Proc.new
or (in Ruby 1.9) the "stabby lambda" syntax. But on its own, they aren't objects.
Another example are operators.
How can I determine if an object is a PORO or not?
There's really no such thing as a "plain old Ruby object" as everything, literally everything is an object anyway, so there's no concept of "plain". How would you define non-plain?
This is unlike JavaScript where there are simple object primitives and then others that are more formally declared using things like the ES6 class
construct. A JavaScript object being passed around as a container is a common pattern. The equivalent in Ruby would be passing a complex data structure like a Hash with potentially nested elements.
If you want to know the class of an object:
@a_form.class
If you want to know what this inherits from:
@a_form.superclass # Immediate parent
@a_form.class.ancestors # All base classes and mixin modules
There are anonymous classes in Ruby, those made with Class.new
, and objects created with those are about as plain as you can get, but they're still first-class objects.
Related Topics
How to Run a Single Test/Spec File in Rspec
Removing a Model in Rails (Reverse of "Rails G Model Title...")
How to Get a Single Column's Values into an Array
Rails - Rspec - Difference Between "Let" and "Let!"
Get Sidekiq to Execute a Job Immediately
Undefined Method with "_Path" While Using Rails Form_For
How to Use Gems Not in a Gemfile When Working with Bundler
What Is the Best Practice When It Comes to Testing "Infinite Loops"
How to Create an Anchor and Redirect to This Specific Anchor in Ruby on Rails
Array of Indexes to Array of Ranges
Traversing a Hash Recursively in Ruby
Cannot Login to Amazon with Ruby Mechanize
Hacking Activerecord: Add Global Named Scope