[ruby-talk:444208] Trivia: Debugging closures

If there's an interest in such posts, I will write more. The idea is
that I will provide examples of lesser-known features of Ruby. Later
on, I will do a longer write-up where I will provide a solution and
explain it unless someone will do it before me :slight_smile:

Let's say I have a program I can't modify:

def gen_proc
  secret1 = rand
  secret2 = rand

  proc do
    p secret1 * secret2
    nil
  end
end

my_proc = gen_proc

binding.irb

It will launch an interactive Ruby console. When I type:

my_proc.() # .() is a shortcut of .call()

It will print some number, but each time I will type this expression,
it will be the same number (until I relaunch the console). This number
is derived from `secret1` and `secret2` variables. Those variables are
stored somewhere - but where? What is the best way for me to obtain
values of those variables? Do note that I can't modify the code :slight_smile:

路路路

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

I very much enjoy seeing such things posted here. :slight_smile:

路路路

On 5/11/23, hmdne via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

If there's an interest in such posts, I will write more. The idea is
that I will provide examples of lesser-known features of Ruby. Later
on, I will do a longer write-up where I will provide a solution and
explain it unless someone will do it before me :slight_smile:

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

Hi!

If you run the gen_proc function, then the variables secret1 and secret2 are regenerated again and their values are changed but

if you do "my_proc = gen_proc", then my_proc is the following code
{
   puts secret1 * secret2
   nil
}
....
In this case the variables secret1 and secret2 do NOT change and every time you execute the "my_proc" procedure (which is different from the gen_proc function) it always displays the same result on the screen.
:slight_smile:

Enviado con el correo electr贸nico seguro de Proton Mail.

路路路

------- Original Message -------
El jueves, 11 de mayo de 2023 a las 12:12, hmdne via ruby-talk <ruby-talk@ml.ruby-lang.org> escribi贸:

If there's an interest in such posts, I will write more. The idea is
that I will provide examples of lesser-known features of Ruby. Later
on, I will do a longer write-up where I will provide a solution and
explain it unless someone will do it before me :slight_smile:

Let's say I have a program I can't modify:

`ruby def gen_proc secret1 = rand secret2 = rand proc do p secret1 * secret2 nil end end my_proc = gen_proc binding.irb`

It will launch an interactive Ruby console. When I type:

`ruby my_proc.() # .() is a shortcut of .call()`

It will print some number, but each time I will type this expression,
it will be the same number (until I relaunch the console). This number
is derived from `secret1` and `secret2` variables. Those variables are
stored somewhere - but where? What is the best way for me to obtain
values of those variables? Do note that I can't modify the code :slight_smile:
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

Yes. The question is now, if I have that `my_proc` variable, how can I
extract `secret1` and `secret2` from it.

路路路

On Fri, 2023-05-12 at 06:43 +0000, iloveruby via ruby-talk wrote:

Hi!

If you run the gen_proc function, then the variables secret1 and
secret2 are regenerated again and their values are changed but

if you do "my_proc = gen_proc", then my_proc is the following code
{
puts secret1 * secret2
nil
}
....
In this case the variables secret1 and secret2 do NOT change and
every time you execute the "my_proc" procedure (which is different
from the gen_proc function) it always displays the same result on the
screen.
:slight_smile:

------- Original Message -------
El jueves, 11 de mayo de 2023 a las 12:12, hmdne via ruby-talk > <ruby-talk@ml.ruby-lang.org> escribi贸:

> If there's an interest in such posts, I will write more. The idea
> is
> that I will provide examples of lesser-known features of Ruby.
> Later
> on, I will do a longer write-up where I will provide a solution and
> explain it unless someone will do it before me :slight_smile:
>
> Let's say I have a program I can't modify:
>
> `ruby def gen_proc secret1 = rand secret2 = rand proc do p secret1
> * secret2 nil end end my_proc = gen_proc binding.irb`
>
> It will launch an interactive Ruby console. When I type:
>
> `ruby my_proc.() # .() is a shortcut of .call()`
>
> It will print some number, but each time I will type this
> expression,
> it will be the same number (until I relaunch the console). This
> number
> is derived from `secret1` and `secret2` variables. Those variables
> are
> stored somewhere - but where? What is the best way for me to obtain
> values of those variables? Do note that I can't modify the code :slight_smile:
>

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

Fun puzzle, thanks :slight_smile: Spoiler
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
my_proc.binding gives you the execution context, and from there you have
my_proc.binding.local_variables to list the local variables in scope, and
my_proc.binding.local_variable_get(:secret1) to get at the value.

martin

路路路

On Thu, May 11, 2023 at 4:14 AM hmdne via ruby-talk < ruby-talk@ml.ruby-lang.org> wrote:

If there's an interest in such posts, I will write more. The idea is
that I will provide examples of lesser-known features of Ruby. Later
on, I will do a longer write-up where I will provide a solution and
explain it unless someone will do it before me :slight_smile:

Let's say I have a program I can't modify:

def gen_proc
  secret1 = rand
  secret2 = rand

  proc do
    p secret1 * secret2
    nil
  end
end

my_proc = gen_proc

binding.irb

It will launch an interactive Ruby console. When I type:

my_proc.() # .() is a shortcut of .call()

It will print some number, but each time I will type this expression,
it will be the same number (until I relaunch the console). This number
is derived from `secret1` and `secret2` variables. Those variables are
stored somewhere - but where? What is the best way for me to obtain
values of those variables? Do note that I can't modify the code :slight_smile:
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info --
Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org