Everything is an object in ruby
puts self
=> main
puts self.class
=> Object
What behind the scene happening is:
main = Object.new
class Dog
def bark
self
end
end
d = Dog.new
puts d == d.bark //true
class Dog
def self.bark
self
end
end
d = Dog.new
puts d == d.bark //true
class Dog
def << self
def bark
end
def chew
end
def fetch
end
end
end
all methods inside of def << self are class-level methods
students = ["zain", "moaz", "leo", "ali"];
# One way to implement blocks
def each(array, &block)
i = 0;
while i < array.length
element = array[i]
block.call(element) # one way
i += 1
end
end
each(students) do |student|
puts "element == #{student}"
end
students = ["zain", "moaz", "leo", "ali"];
# Second way to implement block is using yield
def each(array)
i = 0
while i < array.length
element = array[i]
yield element # second way
i += 1
end
end
each(students) do |student|
puts "element == #{student}"
end