Ruby example-dynamic execution, binding, thread

Source: Internet
Author: User
# 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.