# Encoding: UTF-8 # --------- dynamic execution # execute 2 + 2 puts eval "2 + 2" # = "4 # execute 15*2 (q indicates that double quotation marks will be calculated, Q indicates that single quotes are output as they are.) number = 15 code = % q {# {number} * 2} puts codeputs eval (CODE) # = "4 # =" # {number} * 2 # --------- binding_test return bindingendbinding_function = binding_testx = 9 eval ("x = 10") eval ("x = 50 ", binding_function) # process the local variable eval ("puts X") # process the local variable eval ("puts X", binding_function) # EXEC "data in the binding environment. RB "# puts" end "# Ruby threads are not provided by the operating system and executed by the Ruby interpreter # advantages: good cross-platform performance # disadvantages: if you need to call the operating system and wait for a response, the entire Ruby program will be paused # the newly opened thread child = fork do sleep 1 puts "child OK" endputs "waiting for child process" process. wait childputs "end" # thread # create 10 threads to sleep randomly and print threads = [] 10. times do thread = thread. new do 10. times {| I | print I $ stdout. flush # transfer thread execution permission thread. pass sleep 1} End threads <threadendputs thread. list. inspect # Join let the main thread wait until the thread finishes executing threads. each () Do | thread. join (3) End