Translate

print get[:intensity] #=> prints 3000
SourceTranslationState
1007
We can use any name for our key. If information has already been stored with that key, our new `set` will override it:
1008
set :intensity, 1000
set :intensity, 3000
1009
In the above example, as we stored both numbers under the same key, the last call to `set` 'wins', so the number associated with `:intensity` will be `3000` as the first call to `set` is effectively overridden.
1010
Get
1011
To fetch information from the Time State we just need the key we used to `set` it, which in our case is `:intensity`. We then just need to call `get[:intensity]` which we can see by printing out the result to the log:
1012
print get[:intensity] #=> prints 3000
1013
Notice that calls to `get` can return information that was `set` in a previous run. Once a piece of information has been `set` it is available until either the information is overridden (just like we clobbered the `:intensity` value of `1000` to `3000` above) or Sonic Pi is closed.
1014
Multiple Threads
Threads
1015
The main benefit of the Time State system is that it can be safely used across threads or live loops. For example, you could have one live loop setting information and another one getting it:
1016
live_loop :setter do
set :foo, rrand(70, 130)
sleep 1
end
live_loop :getter do
puts get[:foo]
sleep 0.5
end
rrand
1017
The nice thing about using `get` and `set` across threads like this is that it will always produce the same result every time you hit run. Go on, try it. See if you get the following in your log:

Loading…

Loading…

Glossary

Source Translation
No related strings found in the glossary.

Source information

Flags
no-wrap
Source string location
10.1-Set-and-Get.md:42
Source string age
a year ago
Translation file
etc/doc/lang/sonic-pi-tutorial-pt.po, string 1012
String priority
Medium
Failing checks