Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active March 27, 2019 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisonmalone/fd934e8a3a9447901790df0044321d7a to your computer and use it in GitHub Desktop.
Save harrisonmalone/fd934e8a3a9447901790df0044321d7a to your computer and use it in GitHub Desktop.

Lecture ✍️

Yield 🤷‍♂️

  • Kick off with the idea that this lecture will be tough, yield and things like self are hard
  • The key thing i want you to take away from this lecture is why do we need yield
  • It's very unlikely you'll be using yield much in actual ruby challenges but yield is important because it's fundamental to how the ruby language actually works
  • In saying that, a lot of the time as a developer you just use things, you don't have to understand how it really works, just get the job done

Array objects

class Array 
end 
  • The array class is like the classes we've already been building, but it's built into ruby for us, it has these invisible array instance methods
  • Recap on what is an instance method
  • To create an array object we don't have to do .new, we can just say arr = []
  • We don't get that weird object syntax when we create an array, we get an array, ruby does this for us
  • Look at array class docs, all of these methods are done behind the scenes

Adding methods to the array class 🌈

  • We can add to the array class
  • An example
class Array 
  def special_array_thing 
    return 'hello from special array thing'
  end 
end 
p [1,2,3].special_array_thing

Thinking then about how .each works

  • What is self inside of an instance method
  • A try at making .each
class Array 
  def first_try_with_each 
    counter = 0
    while counter < self.length 
      p self[counter]
      counter += 1
    end 
  end 
end 
[1,2,3].first_try_with_each
  • Okay so we still don't really have the same behavior as .each
  • How can we make it work
  • We need yield
  • So what is yield

Yield and blocks

  • The formal explanation = yield invokes a block
  • Okay so whats a block 🙅‍♀️
  • A block is like a method in terms of encasing some code but it's special in that it gives some other programmer who's going to use your methods in their own projects the ability to customize the way the block works
  • An example of this is like a gem, a method call from the gem might allow you to use a block, and the way you choose to program that block is custom to your app
  • .each is a ruby method (array class instance method) which takes a block
  • .map is a ruby method (array class instance method) which takes a block
  • So i'm calling the .each method and then from the do to the end i'm inside of the block, the other syntax is using the curly brackets {}
  • return in blocks

Lets give it a go 💰

  • See ruby_blocks.rb file below for example code
class Array
def special_array_thing
return 'hello from special array thing'
end
def first_try_with_each
counter = 0
while counter < self.length
p self[counter]
counter += 1
end
end
# lets put a yield and block example in action
def test_yield
p 'hello inside the method'
yield
p 'hello im still inside the method'
end
# we can pass to yield an argument, this argument then goes inside the pipes inside of the block, what we name inside the pipes can be anything
def test_yield_with_argumument
p 'hello inside the method'
yield(5)
p 'hello im still inside the method'
end
# now we know that we can pass arguments to yield we can start thinking about how .each actually works in ruby
# we can allow the programmer (us) to do something with each item we iterate through
def my_each
counter = 0
while counter < self.length
yield(self[counter])
counter += 1
end
end
def my_map
new_arr = []
self.each do |element|
new_arr << yield(element)
end
return new_arr
end
end
# testing basic instance methods
p [1,2,3].special_array_thing
[1,2,3].first_try_with_each
# testing blocks and yield
[1,2,3].test_yield do
p 'hello inside the block'
end
[1,2,3].test_yield_with_argumument do |number_five|
p number_five
end
# imitating actual ruby methods (my_each and my_map)
[1,2,3].my_each do |element|
p element * 10
end
times_ten = [1,2,3].my_map do |element|
element * 10
end
p times_ten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment