This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Order | |
| attr_accessor :type, :sub_orders | |
| def initialize type | |
| self.type = type | |
| self.sub_orders = [] | |
| end | |
| # 统计订单及其包含子订单的数量,按照订单类型归类 | |
| def count_items | |
| result = sub_orders.reduce({}) do |result, order| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(multi_try). | |
| -export([dothese/1]). | |
| dothese(FuncArray) -> | |
| Me = self(), | |
| lists:foreach(fun(F) -> spawn(fun() -> F(Me) end) end, FuncArray), | |
| spawn(fun() -> timer:sleep(200), Me ! timeout end), | |
| Result = get_result("",length(FuncArray)), | |
| io:format("result: ~p~n", [Result]). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'action_mailer' | |
| class Mailer < ActionMailer::Base | |
| def contact | |
| mail( | |
| :to => "[email protected]", | |
| :from => "[email protected]", | |
| :subject => "Test") do |format| | |
| format.text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RubyVM::InstructionSequence.compile_option = { | |
| tailcall_optimization: true, | |
| trace_instruction: false | |
| } | |
| eval <<end | |
| def fact(n, result = 1) | |
| if n == 1 | |
| result | |
| else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def foo | |
| 111 | |
| rescue | |
| 222 | |
| ensure | |
| 333 | |
| end | |
| puts foo # got 111 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module MyTool | |
| module Tree | |
| class << self | |
| def loop root, &block | |
| return false unless File.exist? root | |
| block_given? ? exec(root, &block) : collect(root) | |
| end | |
| def exec root, &block |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'thread' | |
| def prime n, concurrent_num=4 | |
| full_list = (2..n).to_a | |
| prime_list = [] | |
| stop_index = (n ** 0.5).floor | |
| last_prime = 2 | |
| while(last_prime < stop_index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './prime.rb' # user defined function | |
| require 'benchmark' | |
| require 'digest/md5' | |
| result = { | |
| 4 => 'ae5161e4645f5cd58897d32f529f3ad5', | |
| 5 => 'cf7b5007830f057a04d2b696527952e7', | |
| 6 => 'a43beaed59a26b8d85868eaa8fa2e972', | |
| 7 => 'c1aa9bda24aa8bc5473ca1f51c9fd106' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def prime n | |
| list = (2..n).to_a | |
| (2...(n ** (0.5)).floor).each do |i| | |
| list.delete_if{|e| e > i && e % i == 0} | |
| end | |
| list | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 王 | |
| 江 | |
| 周 | |
| 胡 | |
| 刘 | |
| 李 | |
| 吴 | |
| 毛 | |
| 温 | |
| 习 |