Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Derek Maffett
    @DerekMaffett
    method(variable)
    Rachel Lanman
    @RSR312-206
    aaaah ok
    Derek Maffett
    @DerekMaffett
    If you're passing in a block which at runtime will be expected to be a block of code which accepts a parameter, you can activate the block with a parameter in the same way
    Rachel Lanman
    @RSR312-206
    so in this case, my method is the block and the variable is 'lunch'
    Derek Maffett
    @DerekMaffett
    Correct
    Rachel Lanman
    @RSR312-206
    but you still need to use .call on the block, right?
    Katie Kim
    @kayeon
    @RSR312-206 sounds good :)
    Derek Maffett
    @DerekMaffett
    Right. I could be wrong on the specifics, but it seems like since you're passing the block into the function with a variable name, you're basically turning it into a proc internally
    Rachel Lanman
    @RSR312-206
    @DerekMaffett answered my own question via trial and error - yes.
    ok that makes sense
    Derek Maffett
    @DerekMaffett
    Procs are called with proc.call(variable)
    Anonymous blocks can also be called with yield(variable)
    Rachel Lanman
    @RSR312-206
    anonymous?
    Derek Maffett
    @DerekMaffett
    blocks are anonymous functions - they don't have names like the methods you define
    So for example
    array.map do |element| 
      element + 1 
    end
    There's a function being passed into map, but it's anonymous
    I could also define that function like this:
    -> { |element| element + 1 }
    That would be a proc
    Rachel Lanman
    @RSR312-206
    aah that's right- the brackets replacing the do end
    Derek Maffett
    @DerekMaffett
    More importantly, I can assign that to a variable.
    x = proc { |e| e + 1 }
    Then you could pass it as a variable
    Rachel Lanman
    @RSR312-206
    and then pas that variable into the block?
    Derek Maffett
    @DerekMaffett
    Hmm
    The example I was trying to use isn't working in irb
    Not sure how the #map function handles proc arguments internally
    Anyways, the basic idea is just that something like { |e| e + 1 } is a function
    When it's passed into another method without first being assigned to a variable, it's referred to as a block, or anonymous function
    When that bit of code is first assigned to a variable, it's a proc
    They're basically the same concept, though - objects containing code which can be passed around.
    If I were to make a method to accept the top example, I could do this:
    def method_accepting_proc(variable, proc)
      proc.call(variable)
    end
    
    method_accepting_proc(2, x) =># 3
    Rachel Lanman
    @RSR312-206
    ok- assigned to a var is a proc, passed into another method as is creates a block?
    Derek Maffett
    @DerekMaffett
    Like that
    This function accepts a named function (proc) as a second argument, and calls it internally (here using the x I defined above)
    Alternatively
    def method_using_block(variable, &block)
      block.call(variable)
    end
    Or:
    def method_using_block(variable)
      yield(variable)
    end
    Both of those would be called like this:
    method_using_block(2) do |x|
      x + 1
    end
    
    =># 3
    Derek Maffett
    @DerekMaffett
    Don't worry if it's hard to wrap your mind around - functional programming can be crazy at times
    Rachel Lanman
    @RSR312-206
    no that makes sense - thank you!
    Derek Maffett
    @DerekMaffett
    :+1:
    Ryan Sobol
    @ryansobol
    @/all let’s meet at 7pm so everyone has time to work on the hw
    Zac Mrowicki
    @zmrow
    should be fun!
    NoahNim
    @NoahNim
    Where will we meet
    My dad might come too cause he is out here visiting
    Ryan Sobol
    @ryansobol
    @/all also want to suggest checking out this excellent Ruby resource https://github.com/neo/ruby_koans