2

I hear "Ruby is pure OOP language" and "everything in Ruby is an object." If it is so, why do we have these two situations?

  1. Functions like puts and print work on a string object. According to OOP rules, the object's method is the one that should manipulate it's state.
  2. I tried to define functions inside irb, and it accepts it. It's not object oriented to write functions because, if it was object oriented, we cannot define functions this way; we should define them in classes as in C#.

Can anyone explain how these two situations fit with the phrase "Ruby is pure OOP language"?

sawa
  • 165,429
  • 45
  • 277
  • 381
user3718463
  • 265
  • 1
  • 7

4 Answers4

6
  1. puts and print do not do anything to the arguments (which are not necessarily strings by the way). They modify the IO streams, and that is where these methods are defined (although syntax sugar in Kernel makes them accessible from almost anywhere).

  2. When there is no explicit class body, the code is interpreted in the context of main object, which belongs to the Object class.

sawa
  • 165,429
  • 45
  • 277
  • 381
2

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

jmm
  • 1,044
  • 2
  • 12
  • 38
1

Also "pure OOP language" is not as strict as it sounds. It's more in terms of how it's implemented and that everything in the end gets evaluated inside an object.

That being said you can do plenty of functional things, including passing around functions(lambdas), currying and lazy evaluation.

Some examples are in this presentation and this example as well.

Stefan Dorunga
  • 679
  • 6
  • 18
  • `functional things, including passing around functions(lambdas)` - all of that are still objects. :) – BroiSatse Oct 20 '14 at 15:59
  • That's why I said it's more about how it's implemented. It's still a functional concept, and you can create functional style programs, which would look like a contradiction but it isn't. I was trying to say that it depends on what you think 'functional' means – Stefan Dorunga Oct 20 '14 at 16:01
0

The method you defined is itself an object, because everything is an object.

2.1.2 :034 > def my_method
2.1.2 :035?>   puts "hi"
2.1.2 :036?>   end
 => :my_method 
2.1.2 :037 > x = method(:my_method)
 => #<Method: Object#my_method> 
2.1.2 :038 > x.call
hi
 => nil 
2.1.2 :039 > x.class
 => Method 

puts is defined on the module Kernel, which is mixed into Object which is inherited by IO. It doesn't manipulate the state of a string, it reads the string and prints it out to a output stream.

philosodad
  • 1,808
  • 14
  • 24
  • 4
    A module is an object. – Stefan Oct 20 '14 at 14:48
  • A module cannot be instantiated, and Kernel doesn't inherit from BasicObject. I'll correct this to read "A module is a class that can't be instantiated", but I still don't see it as an "object", in the sense that a module only has methods and constants, not attributes. – philosodad Oct 20 '14 at 14:55
  • 2
    @philosodad - `Module` (with capital M) is an instance of class called `Class` (which is also its own instance - Ruby weirdness, yeah!). You can create new modules with `Module.new` hence, each module is an instance of Module class and hence it is an object. You can check that `Kernel.is_a? Module` or even `Kernel.is_a? Object` – BroiSatse Oct 20 '14 at 15:09
  • On the other hand, `Module` does not inherit from `BasicObject`, and `BasicObject.is_a? Object` is true, despite the fact that `Object.ancestors.include? BasicObject` is also true. I was trying to simplify Ruby a bit--just following the basic inheritance chain. – philosodad Oct 20 '14 at 15:14
  • 1
    @philosodad - `Module.is_a? BasicObject #=> true` :) – BroiSatse Oct 20 '14 at 15:23
  • I stand corrected. Of course, after playing with irb for a bit, I realized that wasn't the point at all. The method you define in irb IS an object itself. I've voted up all the corrections. – philosodad Oct 20 '14 at 15:30
  • 1
    `my_method.is_a? Object` sends `is_a?` to `my_method`'s return value, i.e. `nil`. – Stefan Oct 20 '14 at 15:31
  • 1
    Stefan is right here. However you are right as well that a method is an object as well, but you prove it incorrectly: `method(:my_method).class #=> Method` BTW. Method is a very interesting class - it is an instance of Class class, however it does not defines neither `allocate` or `new` module methods and hence cannot by instantiated the usual way. – BroiSatse Oct 20 '14 at 15:42
  • This asnswer clearly says that method is not an object http://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not#answer-2602485 – Shri Sep 21 '16 at 06:13