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.