Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby methods without class?

Tags:

ruby

Hey everyone! I was wondering how the methods in Ruby that aren't called with the syntax ClassName.method_name work. Some off the top of my head are puts, print, gets, chomp. These methods can be called without using the dot operator. Why is this? Where do they come from? And how can I see the full list of such methods?

like image 842
Kvass Avatar asked Jun 06 '11 21:06

Kvass


2 Answers

All methods in Kernel will be available to all objects of class Object or any class derived from Object. You can use Kernel.instance_methods to list them.

like image 119
Chris Jester-Young Avatar answered Oct 31 '22 08:10

Chris Jester-Young


They come from the Kernel module that is automatically included for each class. Those

irb(main):001:0> class Foo
irb(main):002:1> end
=> nil
irb(main):003:0> Foo.included_modules
=> [Kernel]
like image 35
xinit Avatar answered Oct 31 '22 08:10

xinit