12. a <=> b :=
if a < b then return -1
if a = b then return 0
if a > b then return 1
if a and b are not comparable then return
nil
13. [1,3,2] <=> [2,2,2]
- Ruby will start comparing each element of
both array from left hand side.
- 1 of left array is smaller than 2 of right
array.
- Hence left array is smaller than right array.
Output will be -1.
15. - "If I have a drawer labelled a would it
make sense to put b in that drawer?"
- The main usage for the === operator is in
case expressions
a === b
17. Diff between a == b and a === b
- (a == b) and (b == a) is always the same
- (a === b) and (b === a) is not always the
same
Integer === 2 #true
2 === Integer #false
26. We just used double splat to capture all
keyword arguments
27. def join_array(arr, elements: , by_char: ‘,’)
arr.take(elements).join(by_char)
end
a = [20, 30, 10, 40]
# optional if there is default set in parameters, in this case it is by_char
p join_array(a, elements: 3)
p join_array(a, elements: 2, by_char: ‘-’)
# order not important
p join_array(a, by_char: ‘-’, elements: 3)
30. def parrot(&what_human_said)
puts “Hey, I’m gonna say what you said”
what_human_said.call
puts “Thank you. Have a nice day”
end
parrot do
puts “I’m not Siri”
puts “I’m green, eeee”
end
32. Sorta like a magic :)
Ruby also lets us do it implicitly
33. def parrot
puts “Hey, I’m gonna say what you said”
yield
puts “Thank you. Have a nice day”
end
parrot do
puts “I’m not Siri”
puts “I’m green, eeee”
end
34. yield
- is not a method but a type.
- yield executes the block.
- faster than previous eg.
36. But, what is proc?
- an object
- with blocks of code
- bound to a set of local variables
37.
38. lambda is just like proc but strict and nice
- lambda checks parameters passed to it
- lambda is not like that adamant child that
forces you out of the movie just because
he didn’t like it.
52. - A curried proc receives some arguments.
- If a sufficient number of arguments are
supplied, it passes the supplied arguments
to the original proc and returns the result.
53. multiple = proc { |x, y| x * y }
multiple.call(2, 3) #=> 6
# here x = 2, y = 3
curry_multiple = multiple.curry
#=> curried the multiple proc
54. multiple_of_2 = curry_multiple.call(2)
# here x is 2, y is not yet passed
# when y is passed, the proc will
# get executed
multiple_of_2.call(3) #=> 6
# here x is 2, y is 3
71. class Politics
def free_scheme
puts “Yo. Get lots of free stuff.”
end
alias_method :money_time, :free_scheme
end
Politics.new.free_scheme # what you see :P
Politics.new.money_time # what they see :P
81. module Watson
refine String do
def palindrome?
self.reverse == self
end
end
end
“Ruby”.palindrome?
#=> NoMethodError: undefined method `palindrome?' for "Ruby":String
#Not available outside the namespace
111. the class to hold the class specific methods
So, actually ruby is making a singleton class for
just like it does for object because class is
also object?
112.
113. invisible to the programmer.
AH! Yes! And The singleton class is typically
But you sorta see in one syntax, which is
115. class << self is like class Singleton(self)
- You want to add a method just to a specific
object. (note: even class is an object)
- But you can’t write method to a object.
- So, you write method to object’s singleton
class like this.
116. class Human;end
matz = Human.new
class << matz
def write
puts “Writing the epic: Ruby”
end
end
matz.write
132. class PA
extend Forwardable
def_delegators :@ceo, :availability, :phone_call, :office_mails
def initialize(ceo)
@ceo = ceo
end
def check_my_mail(mail)
office_mails.include? (mail)
end
end
133. jack_ma = CEO.new(“Jack Ma”)
jack_ma_pa = PA.new(jack_ma)
jack_ma_pa.phone_call do
puts “Hello, mr.jack ma.”
end
jack_ma_pa.check_my_mail(1234)
134. Also look for other awesome classes
- Queue
- Stack
- Thread
- StringScanner
136. class A
def initialize(foo)
@foo = foo
end
end
a = A.new (“cool”)
a.instance_variable_get “foo”
a.instance_variable_set :@foo, “hotl”
a.instance_variables
137. class A
@@b = 10
end
A.class_variable_get “b”
A.class_variables
138. const_get
class A
PI = 3.14
end
A.const_get “PI” # also there is const_set
#only works with the classes not with objects
143. # earlier we saw this code
class Human;end
matz = Human.new
class << matz
def write
puts “Writing the epic: Ruby”
end
end
matz.write
144. # Can also be written as
class Human;end
matz = Human.new
matz.instance_eval do
def write
puts “Writing the epic: Ruby”
end
end
matz.write
145. Since class is also an object
we can use instance_eval on it.
146. # we earlier saw this
class RubyLang; end
RubyLang.define_singleton_method(:minaswan) do
puts “Matz is nice and so we are nice”
end
RubyLang.minaswan
147. # Also can be written as
class RubyLang; end
RubyLang.instance_eval do
puts “Matz is nice and so we are nice”
end
RubyLang.minaswan
149. class Person; end
Person.new.name #=> ERROR
Person.class_eval do
attr_accessor :name
def trumpet
puts “Me..me...me… #{name}”
end
end
person = Person.new
person.name = “Narcissis”
person.trumpet
151. module Mod
CONST = 1
def math
# ...
end
end
Mod.class #=> Module
Mod.constants #=> [:CONST]
Mod.instance_methods #=> [:math]
152. that you can use across multiple classes
Modules are about providing methods
153. - extend
- include
- prepend
- using (refinements)
We already saw about ‘using’
154. module Hipster
def flame_wars
puts “Yo, this lang is better, that lang is worse”
end
end
class Programmer
end
me = Programmer.new
me.extend Hipster
me.flame_wars
155. Can you guess where the
module methods were inserted for object?
156. extend just did that for us :)
To the singleton class of the object.
157. module Hipster
def flame_wars
puts “Yo, this lang is better, that lang is worse”
end
end
class HipsterProgrammer
extend Hipster
end
HipsterProgrammer.flame_wars
158. Yes. We inserted module methods to
the singleton class of class by using extend
159. What if we want to just insert
module methods to the class?
160. module Hipster
def flame_wars
puts “Yo, this lang is better, that lang is worse”
end
end
class HipsterProgrammer
include Hipster
end
me = HipsterProgrammer.new
me.flame_wars
164. prepend is just like include
But prepends itself first in the ancestors chain
165. module Friend
def opinion
puts "This would be best for you :) .."
end
end
class NormalPerson
include Friend
def opinion
puts "I think this would be best for me"
end
end
167. - Though “opinion” method exists in both
classes
- it would start from the first of the ancestors
- i.e NormalPerson in our case
168. module Friend
def opinion
puts "This would be best for you :) .."
end
end
class BFF
prepend Friend
def opinion
puts "I think this would be best for me"
end
end
169. BFF.ancestors
#=> [Friend, BFF, ...]
# This is the order of method look up
BFF.new.opinion
#=> “This would be best for you :) ..”
175. class Errand
def send
puts “Buy stuff”
end
def check
puts “checking…”
end
end
Errand.new.__send__(“check”)
use __send__ if the name send clashes with an existing method in obj
176. Also there are
__ENCODING__ (keyword)
__LINE__ (keyword)
__getobj__ (SimpleDelegator)
__setobj__ (SimpleDelegator)
177. Things we missed
- Operators like &&=, ||=, =~
- File operations
- System calls
- Enumerator methods
- threads
- exceptions, backtraces, benchmarking
- and many more, do check it out