On 28 November 2016 at 07:57, Micky Scandal <mickyscandal / gmail.com> wrote:

> thank you that really clears things up. after I posted that I thought
> about it some more and that's pretty much what I came up with, but your
> explanation was much better (and very amusing).
>
>   It sounds to me like either you're not quite understanding what "store
> a value in a variable" means, or I'm not understanding your question.
>
> My thought process was that since I assigned num to the result of a method
> (rand()) that each time num was called it would run the method instead of
> running only once and assigning the result of that one run to num.
>
>
Here are two analogies that might keep clear why that's not what happens:

For one, instead of rand() imagine it was:
some_big_slow_expensive_function_that_I_only_want_to_calculate_once()

For two, imagine it was: STDIN.read()

In either case you want to get the value once and remember it.

That said, it *is* possible to pass around values that "recalculate" every
time they're used (using lambda functions for one, or using special objects
with custom APIs for another.) Usually, though, we just call the function
again.

Cheers
-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>