Hi Mark,
Glad you enjoyed Pine. A couple more details, because you seem interested:
A Proc isn't quite just a function (I don't think a mere function really
exists in Ruby) -- it's a closure, which means variables from a lexical
scope are bound into it and available in it. For example:
f = begin
a = 0
proc { a += 1 }
end
f.call #=> 1
f.call #=> 2
Blocks also aren't objects but, like methods, they can be:
def g
block_given? ? yield : :no_block
end
g { puts "this block never gets to be an object" }
method_object = method(:g)
g(&method_object) #=> :no_block
That last example shows how you can use a Method object or Proc (or any
object with a to_proc method) as a block with the ampersand operator.
If you followed this, Ruby Quiz might interest you for further exercises.
(There is a book made from the early ones. There were a few iterations.
You'll find archived websites in the Internet Archive and [QUIZ] messages
in the ruby-talk mailing list archive.)
Cheers,
Dave
On Sat, Aug 2, 2014 at 2:49 PM, Marky Mark <[email protected]> wrote:
> Thanks, Chris Pine's explanation is nice and clear, and very
> comprehensible to my C++ mind. A proc is just (!) a function-as-object. And
> a block is just an anonymous proc. Though I'm sure I'm missing some Ruby
> subtleties there. I had overlooked the fact that methods aren't objects and
> hence aren't first-class entities. Hey, I thought *everything* was an
> object in Ruby :-)
>
> His profiling example is something I'm sure I've done in C++ too. Handing
> in functions (ok, function pointers) as things-to-do is pretty standard.
> Though you have to take more care about matching your prototypes just to
> get it past the compiler.
>
> I'm amused by his comment in passing about how nobody ever actually does
> any currying.
>
>
> On Friday, August 1, 2014 10:09:29 PM UTC+10, Dave Burt wrote:
>
>> Hi Mark,
>>
>> First a minor correction: "case a when b ..." is actually "if b === a
>> ..." -- the === method is called on object b with argument a.
>>
>> "yields" is not a clear word to use -- in Ruby it refers to a method
>> calling a passed block.
>>
>> But you're right, you have misunderstood what proc returns. The proc
>> method returns a Proc object, and "Proc objects are blocks of code." The
>> Proc object in your example accepts an argument, calls its "more_blue?"
>> method, and returns the result. That result will be true or false, but the
>> Proc is a essentially function which can be called.
>>
>> Then, also, the Proc has a === method. case...when turns into if like
>> this:
>>
>> if (proc { |color| color.more_blue? }) === color
>> "blueish"
>>
>> Proc#=== returns the result of the Proc when called with the case
>> subject. So to run that when clause, the proc will be called with the color
>> as argument, equivalent to this if statement:
>>
>> if (proc { |color| color.more_blue? }).call(color)
>> "blueish"
>>
>> http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-proc
>> http://www.ruby-doc.org/core-2.1.1/Proc.html.
>>
>> Chris Pine wrote a great intro to blocks and procs in Ruby over a decade
>> ago now, which includes some exercises to play with and cement your
>> understanding:
>> https://pine.fm/LearnToProgram/?Chapter=10
>>
>> Cheers,
>> Dave
>>
>>
>> On Fri, Aug 1, 2014 at 6:01 PM, Marky Mark <[email protected]> wrote:
>>
>>> Um, I just wrote my original question out and stared at it some more and
>>> realised it was a stupid question to begin with :-(
>>>
>>> But the more I think about it the more I'm confused (and maybe this is
>>> where hearing what Tim said at the time would help).
>>>
>>> Conceptually I see "case a when b ... when c ... else ..." as being the
>>> same as "if a === b ... elsif a === c ... else ..." (as indeed slides 67-69
>>> show) but that seems to fail for slide 62's use of proc:
>>>
>>> case color
>>> when proc { |color| color.more_blue? }
>>> "blueish"
>>>
>>> because comparing color with what the proc yields (true or false) surely
>>> doesn't work. Am I misunderstanding what proc yields?
>>>
>>>
>>> On Friday, August 1, 2014 5:08:06 PM UTC+10, Dave Burt wrote:
>>>
>>>> Thanks for the notes, Tim!
>>>>
>>>> Mark, post your case questions here!
>>>>
>>>> On the same topic, here is an article by Why on another feature of
>>>> Ruby's case statements: you can *splat* an array like this:
>>>>
>>>> foo = 3
>>>> ARRAY = [/abc/, Hash, 1..5]
>>>> case foo; when *ARRAY; 'included'; else "doesn't === anything in the
>>>> array"; end
>>>>
>>>> http://viewsourcecode.org/why/redhanded/bits/wonderOfTheWhenBeFlat.html
>>>>
>>>> Cheers,
>>>> Dave
>>>>
>>>>
>>>> On Fri, Aug 1, 2014 at 2:53 PM, Marky Mark <[email protected]> wrote:
>>>>
>>>>> Just slides though, no soundtrack? Familiar with case statements as I
>>>>> am in general, I can't help thinking I'm missing a little of the
>>>>> subtlety/reasoning w.r.t Ruby. I do have one or two stupid questions.
>>>>> Should I post here or ask you privately?
>>>>>
>>>>>
>>>>> On Thursday, July 31, 2014 3:04:32 PM UTC+10, Tim Moore wrote:
>>>>>>
>>>>>> Yes, I'll echo Dave's thoughts: thanks to the organisers and the
>>>>>> substitute host!
>>>>>>
>>>>>> My slides are up at https://speakerdeck.com/tim
>>>>>> moore/rubys-magical-case-statement and http://www.slideshare.net/
>>>>>> timothymoore/rubys-magical-case-statement (take your pick)
>>>>>>
>>>>>> On Thursday, July 31, 2014 8:52:42 AM UTC+10, Dave Burt wrote:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> Thanks to the organisers and speakers for another great evening.
>>>>>>>
>>>>>>> May I ask again for links to any of the talks that are available?
>>>>>>>
>>>>>>> I think Duncan referenced his Github repo https://github.com/duncan
>>>>>>> -bayne/presentations
>>>>>>>
>>>>>>> Cheers,
>>>>>>> Dave
>>>>>>>
>>>>>>>
>>>>>>> On Fri, Jul 25, 2014 at 1:17 PM, Mario Visic <[email protected]>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Hi All
>>>>>>>>
>>>>>>>> The Melbourne Ruby meet will be happening next wednesday evening
>>>>>>>> (the 30th July), with our usual array of entertaining talks, delicious
>>>>>>>> food, tasty beverages, and delightful company (that’s you!).
>>>>>>>>
>>>>>>>> WHEN: Wednesday 30th July, arrive at 6pm for a 6:30pm start,
>>>>>>>> wrapping up around 9pm.
>>>>>>>> WHERE: Inspire9, Level 1, 41-43 Stewart Street
>>>>>>>> WHAT: Three speakers, plus socialising, good food (not pizza) and
>>>>>>>> drinks.
>>>>>>>>
>>>>>>>> Our line-up this month is:
>>>>>>>> Jordan Lewis + Luke Arndt - Styleguide driven development
>>>>>>>> Duncan Bayne - There’s no such thing as magic - tools and
>>>>>>>> techniques for Ruby developers
>>>>>>>> Tim Moore - Case Statements and ===
>>>>>>>>
>>>>>>>> All of these details are repeated on Meetup, for those who prefer
>>>>>>>> that as their source of event information:
>>>>>>>> http://www.meetup.com/Ruby-On-Rails-Oceania-Melbourne/events
>>>>>>>> /175705562/
>>>>>>>>
>>>>>>>> Yet again, food is sponsored by Envato, Lookahead Search and
>>>>>>>> Redbubble, the drinks by Zendesk, and the space by Inspire9.
>>>>>>>>
>>>>>>>> If you've any questions or thoughts, get in touch - we're looking
>>>>>>>> forward to seeing you all there :)
>>>>>>>>
>>>>>>>> --
>>>>>>>> Mario & Pat
>>>>>>>>
>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Ruby or Rails Oceania" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to [email protected].
>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>> Visit this group at http://groups.google.com/group/rails-oceania.
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Ruby or Rails Oceania" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>>
>>>>> Visit this group at http://groups.google.com/group/rails-oceania.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>
>>
--
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.