25
loading...
This website collects cookies to deliver better user experience
self
# => main
self.class
# => Object
nil.class
# => NilClass
true.class
# => TrueClass
main
thing? Well, main
is the default top level context provided to us in Ruby. Since everything is an object, we call this the global object. But even our global object is an instance of Object
.Object
is an instance of the class Class
!self.class.class
# => Class
class Class
alias prev_new new
def new(*args)
print "Creating a new #{self.name} class. \n"
prev_new(*args)
end
end
class Text
end
t = Text.new
# => ...
# => Creating a new Text class.
function ourFunction():void {
console.log("hello");
}
let variable = ourFunction
variable()
// "hello"
// => undefined
def method
puts "hello"
end
variable = method
variable()
# NoMethodError (undefined method `variable' for main:Object)
variable
# => nil
variable
is assigned the return value of method
. As we call method
, it is implicitly invoked. Calling method
, the method is invoked, the same as method()
. In our current paradigm, there is no way we can pass the function itself to another variable.Higher-order function: a function that takes a function as a parameter or returns a function.
function multiplier(number1: number): (number2: number) => number {
return function(number2: number): number {
return number1 * number2
}
}
let doubler = multiplier(2)
doubler(6)
// => 12
procs
. A Proc
is a special type of Ruby object that allows us to store a block of code as an object. As we instantiate our Proc
instance, we can pass a block to the instantiation immediately following any method parameters.print_greetings = Proc.new() do
puts "Welcome!"
puts "Bonjour!"
puts "¡Bienvenidas!"
end
print_greetings
# => #<Proc:0x00007fe0ff042a08>
.call
.yield
[]
.call
with syntactic sugar .()
print_greetings.call
print_greetings.yield
print_greetings[]
print_greetings.()
# Welcome!
# Bonjour!
# Bienvenidas!
# => nil
*
to allow an undetermined amount of arguments to be captured.nil
will be assigned to the parametermultiply_by_two = Proc.new do |item|
item * 2
end
Proc
back into a block, we can use the &
operator inside of our method calls that expect a block. The two examples below are equivalent.[1,2,3,4,5].map do |item|
item * 2
end
[1,2,3,4,5].map(&multiply_by_two)
# => [2, 4, 6, 8, 10]
.ourFunction(1, 2, &multiply_by_two)
[1,2,3,4,5].map(&multiply_by_two, &multiply_by_three)
would produce a SyntaxError.def multiplier(number1)
Proc.new {|number2| number1 * number2}
end
doubler = multiplier(2)
# => #<Proc:0x00007fe7719b91b8>
doubler.call(6)
# => 12
yield
keyword. Allowing you to inject parts of your .erb
inside of templates. Similarly, in plain old Ruby, the yield
pauses execution of the current code and yields to the block that was passed.def our_method
puts "top of method"
yield
puts "bottom of method"
end
our_method {puts "we are inside the block"}
# top of method
# we are inside the block
# bottom of method
# => nil
def our_method_w_parameters
puts "top of method"
yield("karson", "nyc")
puts "bottom of method"
end
our_method_w_parameters do |name, loc|
puts "my name is #{name}, and I live in #{loc}"
end
# top of method
# my name is karson, and I live in nyc
# bottom of method
# => nil
yield
.self
. Let's take a look at the example below.class Person
attr_accessor :name, :loc
def initialize(name, loc)
@name = name
@loc = loc
end
def ex_block
yield
end
end
k = Person.new("karson", "nyc")
# => #<Person:0x00007fded014edb0 @name="karson", @loc="nyc">
k.ex_block {puts self.name, self.loc}
# NoMethodError (undefined method `name' for main:Object)
self
refers to the main
object which tells us that we are in the global scope. If we want to bind self
to the instance where the block is called, it must be defined upon instantiation.class Person
attr_accessor :name, :loc, :instance_proc
def initialize(name, loc)
@name = name
@loc = loc
@instance_proc = Proc.new() do
puts self.name, self.loc
end
end
def ex_proc(&proc)
yield
end
end
k = Person.new("karson", "nyc")
# => #<Person:0x00007ff6aa94c228 @name="karson", @loc="nyc", @instance_proc=#<Proc:0x00007ff6aa94c1d8>>
k.ex_proc(&k.instance_proc)
# karson
# nyc
Procs
and blocks
, let's re-create the method .map
on our custom class MArray
.class MArray < Array
def initialize(*args)
super(args)
end
def map(&block)
newMArr = MArray.new()
for element in self
newMArr << yield(element)
end
newMArr
end
end
m = MArray.new(1, 2, 3, 4, 5)
# =>[1, 2, 3, 4, 5]
m.map { |item| item * 2 }
# => [2, 4, 6, 8, 10]