Damir Svrtan
Methods you thought were keywords
Builtin function in Python and PHP
But a regular method in Ruby
print 'Starting our program!'
class User
print 'Defining the User class'
def initialize
print 'Registering a new user'
end
end
class User
def initialize(name)
@name = name
end
def name
@name
end
end
borko = User.new('Borko')
User.ancestors
=> [User, Object, Kernel, BasicObject]
class Student < User
end
Student.ancestors
=> [Student, User, Object, Kernel, BasicObject]
Student.superclass
=> User
User.superclass
=> Object
Object.superclass
=> BasicObject
BasicObject.superclass
=> nil
In it's class and all the ancestors of course!
borko = User.new('Borko')
zdenko = User.new('Zdenko')
def borko.sing
puts "I'm singing in the rain!"
end
borko.sing
=> "I'm singing in the rain!"
zdenko.sing
=> undefined method `sing' for #<User:0x007fc8320bc528>
def borko.sing
puts "I'm singing in the rain!"
end
class User
def self.new_from_omniauth(email)
...
end
end
class User
class << self # What is self?
# Defining the method on the eigenclass of self
def new_from_omniauth(email)
...
end
end
end
class User
class << self
def new_from_omniauth(email)
...
end
end
class << User
def new_from_omniauth(email)
...
end
end
def self.new_from_omniauth(email)
...
end
def User.new_from_omniauth(email)
...
end
end
class << User
def new_from_omniauth(email)
...
end
end
def User.new_from_omniauth(email)
...
end
show-source Object#print
From: io.c (C Method):
Owner: Kernel
Visibility: private
Number of lines: 6
static VALUE
rb_f_print(int argc, VALUE *argv)
{
rb_io_print(argc, argv, rb_stdout);
return Qnil;
}
Object.ancestors.include? Kernel
=> true
print 'Starting our program!'
class User
print 'Defining the User class'
def initialize
print 'Registering a new user'
end
end
User
#<User:0x007f987>
main
class User
end
User.class
=> Class
User.class.ancestors.include? Object
=> true
User.class.private_instance_methods.include? :print
=> true
borko = User.new
borko.class
=> User
borko.class.ancestors.include? Object
=> true
borko.class.private_instance_methods.include? :print
=> true