Fun puzzle, thanks :) 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 :)

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 :)
 ______________________________________________
 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 -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-talk.ml.ruby-lang.org/