10

So, I'm curious as to how Ruby is a fully object oriented language. I stumble over one problem that isn't really clear to me.

If I define a function as follows

def foo(text)
  print text
end

and I define the function outside of a class, how is this function an object? I realize that I can call

foo.class

And I get NilClass. Does this mean that foo is an instance of NilClass? And if it is, what does it mean exactly when I call

foo "hello world"

If foo is an object, what method am I calling when I make the statement as above. Also, if it an object, does that mean I can modify it and add another method to it (say bar) where I could possibly make the following statment:

foo.bar(some variables)

Sorry, I'm just a little confused on this point. Any clarification is very much appreciated! Thanks!

Nikhil
  • 5,705
  • 1
  • 32
  • 30
  • 1
    possible duplicate of [Methods in Ruby: objects or not?](http://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not) – OscarRyz Jun 24 '10 at 00:12

6 Answers6

12
  • User defined global functions (top-level functions) are instance methods of Object (even though the class of self is not Object).
  • Top-level methods are always private.
apaderno
  • 28,547
  • 16
  • 75
  • 90
  • 2
    ok, that makes sense. So functions are not objects, but private methods of the Object class. Thanks! –  Jun 24 '10 at 00:29
11

As Wikipedia states:

All methods defined outside of the scope of a particular object are actually methods of the Object class.

Ruby is actually "multi-paradigm". It supports object-oriented, functional, imperative (and a few others) paradigms.

Being "fully object-oriented" doesn't mean you only support the object-oriented paradigm. As long as you support all the features that make up object-oriented programming (classes, instances, polymorphism, etc) then you can still support additional paradigms and still be "fully object-oriented".

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
  • So, does that meant that the following would be valid: o = Object.new o.foo ?? (after defining the foo object of course) –  Jun 24 '10 at 00:13
  • And I understand what "fully object oriented" means (at least I'm pretty sure), but doesn't that still meant that EVERYTHING should be an object. So, my question isn't if Ruby can be used for various paradigms, but from an object perspective are methods objects and if so, what does that imply. –  Jun 24 '10 at 00:19
  • No, they aren't. See: http://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not – Owen S. Jun 24 '10 at 00:23
  • @Owen: To what are you referring with that comment? That global methods are not actually methods on the 'Object' class? That's certainly not what the link you provided says... – Dean Harding Jun 24 '10 at 00:33
  • @Dean: To the question of whether methods are objects in and of themselves. Methods with a capital M are; methods as described here aren't. – Owen S. Jun 24 '10 at 20:57
  • @Dean: Ah, I see the confusion. My comment was a reply to John's comment. Sorry I didn't make that clear! – Owen S. Jun 24 '10 at 21:08
7

foo.class first calls the method foo, which returns nil, and then calls the method class on the object returned from foo, namely nil.

In pseudocode notation, evaluating the code step-by-step:

foo.class
==> { print text }.class
==> { nil }.class
==> nil.class
==> NilClass
yfeldblum
  • 65,165
  • 12
  • 129
  • 169
2

You can get a method as an object. To use your example:

def foo(text)
  print text
end

and then expand upon it:

method_as_object = method(:foo)
method_as_object.call('bar') #=> bar

Typically though, when you define a method, you just define it as a method of the current scope (which is by default the Object class)

darkliquid
  • 4,003
  • 1
  • 25
  • 17
0

To expand on Justice's example, you can take this even further.

def foo
    puts "foo"
end

foo.class
==> NilClass
NilClass.class
==> Class
Class.superclass
==> Module
Module.superclass
==> Object
Object.superclass
==> BasicObject

Everything is an instance of class BasicObject at the very least.

0

< Is Class declaration an eyewash in ruby? Is everything really object oriented? >

Following link best explains how ruby is fully Object oriented so much so that the basic constructs like class Someclass are creating objects from objects.

Community
  • 1
  • 1
pankajdoharey
  • 1,562
  • 19
  • 30