Skip to content
Steve53 edited this page Sep 1, 2015 · 2 revisions

View the source code.

File

ruby hello-world.rb

Interpreter

$ irb
irb(main):001:0> puts "Hello world"
Hello world
=> nil
irb

Prints Hello world.

puts is a method in the Kernel module.

From the Kernel documentation:

  • The Kernel module is included by class Object, so its methods are available in any Ruby object.
  • The Kernel instance methods are documented in class Object while the module methods are documented here. These methods are called without a reveiver and thus can be called in functional form.

Methods in the Kernel module are sometimes called built-in functions. See Ruby Built-in Functions.

puts is a module method, as opposed to an instance method, in the Kernel module.

We often see it written, "Everything in Ruby is an object." But that is an over simplification. See Almost everything is an object.

So is the puts method an object? There is some debate about this.

I am going to say puts is not an object. If it were an object, it would be an instance of a class. And we would be able to determine its class with puts.class.

irb(main):018:0* puts.class

=> NilClass

We can construct a Method object for the :puts symbol. And we can see that the Method object is an instance of class Method, but that is not the same as saying puts is an instance of class Method.

irb(main):019:0> m = method(:puts)
=> #<Method: Object(Kernel)#puts>
irb(main):020:0> m.class
=> Method

Use the owner method to see that puts is in the Kernel module:

irb(main):004:0> method(:puts).owner
=> Kernel

puts is not an instance method:

irb(main):005:0> Object.puts 'hello'
NoMethodError: private method `puts' called for Object:Class
from (irb):5
from /usr/bin/irb:12:in `<main>'

puts is a module method:

irb(main):006:0> Kernel.puts 'hello'
hello
=> nil

Clone this wiki locally