tags:

views:

236

answers:

5

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!

+6  A: 

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
So, does that meant that the following would be valid: o = Object.new o.foo ?? (after defining the foo object of course)
John
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.
John
No, they aren't. See: http://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not
Owen S.
@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
@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.
@Owen: And I never said they were...
Dean Harding
@Dean: Ah, I see the confusion. My comment was a reply to John's comment. Sorry I didn't make that clear!
Owen S.
+10  A: 
  • User defined global functions (top-level functions) are instance methods of Object (even though self is not Object).
  • Top-level methods are always private.
kiamlaluno
ok, that makes sense. So functions are not objects, but private methods of the Object class. Thanks!
John
+4  A: 

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
Justice
A: 

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.

Chris G.
my ruby 1.8.7 show `nil` when `Object.superclass`
ohho
A: 

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