Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Rachel Lanman
    @RSR312-206
    hi class- if anyone's around, just wondering if there are any tips for Class 8, ex 3 homework? I'm stuck on where to assign the block_param to 'lunch'.
    Derek Maffett
    @DerekMaffett
    Let me take a look at the assignment really quick
    Okay, so you're trying to figure out how to define a method which calls a block with a parameter?
    Rachel Lanman
    @RSR312-206
    right, I'm already calling the block in my method, but not sure how to then call the same block, but now with a parameter
    Derek Maffett
    @DerekMaffett
    You're using yield, right?
    Rachel Lanman
    @RSR312-206
    i'm using block.call
    Derek Maffett
    @DerekMaffett
    Oh, okay. I usually use #call on procs, but I guess that works too
    Rachel Lanman
    @RSR312-206
    ah ok
    Derek Maffett
    @DerekMaffett
    No big deal, I just looked it up, and it works the same way
    Rachel Lanman
    @RSR312-206
    i tried yield on some earlier exercises, and I thought rubocop may have taken issue, but I cant remember
    Derek Maffett
    @DerekMaffett
    I don't think rubocop dislikes yield, but I could be wrong. It might also have to do with whether you reference the block being passed in as a parameter or just leave no reference to it in the method definition
    Either way, you're calling a block of code to be run.
    How would you normally pass a parameter to a block of code being run (you do it all the time with normal methods)?
    Rachel Lanman
    @RSR312-206
    right
    wouldn't the parameter be within the method like this: method(parameter) ?
    Katie Kim
    @kayeon
    @/all Cactus tomorrow! happy hour ends @ 6pm tho. will be workin' on homework til about 5ish or so. who's all in? (so far, Noah, Stephanie, Zac, Ryan, Jane, Me) Dave? Patrick? Rachel? Tyler? Kim? Derek? etc etc? :) I mean... @ryansobol - can we just grab a 6pack and meet on 2nd floor?? There's always great drinkin' places on the Hill too...
    Rachel Lanman
    @RSR312-206
    I work till 5, and will then probably need to finish up some homework, so @kayeon I'll text you when I'm done around 7 and see if the group is still there
    Derek Maffett
    @DerekMaffett
    You can define a regular method like that. Importantly, though, when you call a method, you pass in parameters that way.
    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.