Examples of 'Things' That Are Not Objects in Ruby

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.

Methods in Ruby: objects or not?

Methods are a fundamental part of
Ruby's syntax, but they are not values
that Ruby programs can operate on.
That is, Ruby's methods are not
objects
in the way that strings,
numbers, and arrays are. It is
possible, however, to obtain a Method
object that represents a given method,
and we can invoke methods indirectly
through Method objects.

From The Ruby Programming Language:

alt text

Is everything an object in ruby?

Depends on what you mean by "everything". Fixnums 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).

Non-object-oriented aspects in ruby

puts() is a method in the IO class. See http://www.ruby-doc.org/core-2.1.3/IO.html#method-i-puts

IRB is a module, so it's an Object too. See http://ruby-doc.com/docs/ProgrammingRuby/html/irb.html

Operators && and || are objects in ruby?

It depends on how you define "everything". I personally would never claim that each individual concept in ruby is an object. I suppose there isn't really a good definition to describe how ruby works. I think you just have to accept the way ruby does it, which will become plain as you use it.

If you want to see if something is an object, try assigning it to a variable, or calling a method on it. You'll get an error if you try to do that with an operator. A notable exception is methods, which aren't objects but return objects.

Note that the concept of an object is somewhat abstract. Two variables can point to the same object. Every object is represented by an object_id. You might think of an object_id like a location in memory. Or, you can think of an object as a house, and multiple address books can contain the house's address.

# point 2 variables to the same object
>> s = t = 's'
=> "s"
>> s.object_id
=> 70269794388360
>> t.object_id
=> 70269794388360

# get the object by id (for demonstration only; I don't recommend using this in application code)
>> ObjectSpace._id2ref(70269794388360)
=> "s"

# modifying the object. both variables "see" the change
>> s << '_'
=> "s_"
>> t
=> "s_"

Continuing the house analogy, appending to a string via << is like bringing a new chair into the house. Everyone who follows their address book to that house (using the address) will see that the house (object) has the new chair.

The ruby language designers did not see a reason to allow treating operators like && as objects. Can you see any reason to allow it?

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 ;-)

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.

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 of Class, how can it be both a superclass of Class 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 always Object. [Note: there may be implementation-defined superclasses of Object, but eventually, there will be one which doesn't have a superclass.]
  • Object is an instance of Class, which is a subclass of Object (which means that indirectly Object is an instance of Object itself)
  • Class is a subclass of Module, which is an instance of Class
  • Class is an instance of Class

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.

What is the class of if/unless etc

Considering that everything in Ruby is an object

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 if if were 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?



Related Topics



Leave a reply



Submit