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
| # -*- encoding : utf-8 -*- | |
| require 'debugger' | |
| cal = lambda do |exp, stack=[]| | |
| return exp if exp =~/^\d+$/ | |
| exp.scan(/((?:\d+)|[\*\+\/-]|(?:\((?:(?:\d)|[\*\+\/-])+\)))/) do |match| | |
| word = match.first.to_s | |
| if word =~ /^(\d+)$/ | |
| stack.unshift $1 |
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
| regex = /^(?<stack>{(\g<stack>|(?<num>\d))})$/ | |
| "{{2}}".match regex #<MatchData "{{2}}" stack:"{{2}}" num:"2"> |
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 Array | |
| def bindex element, lower = 0, upper = length - 1 | |
| return if lower > upper | |
| mid = (lower + upper) / 2 | |
| element < self[mid] ? upper = mid - 1 : lower = mid + 1 | |
| element == self[mid] ? mid : bindex(element, lower, upper) | |
| end | |
| end | |
| class Array |
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
| # Sample implementation of quicksort and mergesort in ruby | |
| # Both algorithm sort in O(n * lg(n)) time | |
| # Quicksort works inplace, where mergesort works in a new array | |
| def quicksort(array, from=0, to=nil) | |
| if to == nil | |
| # Sort the whole array, by default | |
| to = array.count - 1 | |
| 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
| 1999995.99.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse |
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
| (-> | |
| rightSideTop = $('#right-side').offset().top | |
| $(window).scroll(-> | |
| if $(this).scrollTop() > rightSideTop | |
| $('#right-side').css({ | |
| 'position': 'fixed' | |
| 'top': 0 | |
| }) | |
| else | |
| $('#right-side').css('position', 'static') |
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 Object | |
| def deep_map(&block); yield(self) end | |
| end | |
| class Array | |
| def deep_map(&block); map{|e| e.deep_map(&block)} end | |
| end | |
| or |
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
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| # 有关 Fiber 的解释: (按照数据流的方向分为两部分) | |
| # 在 `主线程' 中使用 resume 方法来启动(或继续执行)一个 `纤程'. | |
| # 1. 第一次调用 fiber.resume, 会启动一个纤程, | |
| # 如果 resume 调用时提供了实参, 会作为代码块形参传入代码块. | |
| # 2. 如果非第一次调用 fiber.resume, 即, `恢复' 一个纤程, 会做两件事: | |
| # - 从上次离开纤程的那个位置(调用 Fiber.yield 离开纤程的那个位置), 恢复纤程的执行. |
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
| #removing the space which is NBSP | |
| " B.我国人民民主具有广泛性"..gsub(/[[:space:]]/, '') | |
| #or | |
| NBSP = "\u00a0" | |
| str.gsub(NBSP, '') |
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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'daemons' | |
| dir = File.expand_path(File.join(File.dirname(__FILE__), '..')) | |
| daemon_options = { | |
| :multiple => false, | |
| :dir_mode => :normal, | |
| :dir => File.join(dir, 'tmp', 'pids'), | |
| :backtrace => true |