
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. :-)
------- 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 :)
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 :)