Ruby 在IE命令中预览(Windows本机版)

#!c:/ruby/bin/rubyw.exe
require 'win32ole'
require 'timeout'

filename = ENV['TM_FILEPATH'].chomp.gsub(/\\/, '/').capitalize
url = "file:///" + filename

# Methods
def openNewBrowser(url)
  begin
  Timeout::timeout(5) {
    ie = WIN32OLE.new('InternetExplorer.Application')
    ie.Navigate("#{url}")
    sleep('.2'.to_f) while ie.ReadyState != 4
    ie.Visible = 'true'
  }
  rescue Timeout::Error
    puts "Unable connect to #{url} in a reasonable time"
  end
end

def refreshIfOpen(url)
   shell = WIN32OLE.new('Shell.Application')
   shell.Windows.each do | window |
    if window.LocationUrl == "#{url}"
        window.refresh()
        return true
    end
   end
return false
end

# Main
unless refreshIfOpen(url)
   openNewBrowser(url)
end

Ruby 剥壳

class CommandError < RuntimeError
end

class BaseCmd
  attr_accessor :uid, :done, :status
end

class Sheller
  attr_accessor :continious

  def initialize(bin = "bash")
    @sleep_time = 0.001
    @input_pipe = IO.pipe
    @output_pipe = IO.pipe
    @error_pipe = IO.pipe
    @invalid = false

    fork do
      @input_pipe[1].close
      @output_pipe[0].close
      @error_pipe[0].close

      $stdin.reopen(@input_pipe[0])
      $stdout.reopen(@output_pipe[1])
      $stderr.reopen(@error_pipe[1])

      exec(bin)
    end

    @input_pipe[0].close
    @output_pipe[1].close
    @error_pipe[1].close
  end

  def low_write(data)
    printf "#{data}" if $DEBUG
    @input_pipe[1].write(data)
  end

  def write(data)
    low_write(data)
    low_write("echo -e \"\027:$?\" > /dev/stderr\n")
  end

  def poll
    clean = true

    if svrs = IO.select([@output_pipe[0]], nil, nil, 0)
      svrs[0].each { |io|
        clean = false
        line = io.readline.chomp
        got_stdout(line)
      }
    end

    if svrs = IO.select([@error_pipe[0]], nil, nil, 0)
      svrs[0].each { |io|
        clean = false
        line = io.readline

        if line[0] == 23
          md = line.match(/^.:(\d+)$/)
          if md
            @pending_cmd.status = md[1].to_i
            raise CommandError if @pending_cmd.status != 0 && @continious
          end
        else
          got_stderr(line)
        end
      }
    end

    if @pending_cmd.status and clean
      @pending_cmd.done = true
    end
  end

  def read_loop
    while !@pending_cmd.done
      poll
      sleep(@sleep_time)
    end
  end

  def do(string)
    @pending_cmd = BaseCmd.new
    begin
      write("%s\n" % string)
      read_loop
    rescue EOFError,Errno::EPIPE => error
      Process.wait
      # $?.exitstatus
      @invalid = true
      return nil
    end
    return @pending_cmd
  end

  def info(string)
    puts "# %s" % [string]
  end

  def got_stdout(line)
  end

  def got_stderr(line)
  end

end

Ruby 在UTC时间探索to_s(基数)

t = Time.now.utc
puts t
2.upto(36) { |r| puts r.to_s + " : " + t.to_i.to_s(r) }

Ruby 逗号分隔占位符

values = [1, 2, 3]
# => [1, 2, 3]

s = '?' * values.size
# => "???"

csv = s.split(//).join(',')
# => "?,?,?"

Ruby 通过宝石安装(HPricot)

gem install hpricot 
http://code.whytheluckystiff.net/hpricot/wiki/InstallingHpricot

Ruby Rake Migrate(最新方法)

rake db:migrate

Ruby Rake Migrate(Backward或Previous Version)示例

rake db:migrate VERSION=0

Ruby 通胀计算器

require 'rubygems'
require 'net/http'
require 'hpricot'

if (ARGV.size != 2) 
    puts "usage: inflate.rb original-cost from-year"
    exit(1)
end

@original_cost = ARGV[0]
@from_year     = ARGV[1]
@this_year     = Date.today.year

res = Net::HTTP.post_form(URI.parse("http://data.bls.gov/cgi-bin/cpicalc.pl"),
            { 'cost1' => @original_cost,
              'year1' => @from_year,
              'year2' => @this_year,
              'submit' => 'Calculate' }
            )
doc = Hpricot(res.body)

puts doc.search("//span[@id='answer']").inner_html

Ruby 在路径中找到并播放具有给定模式的mp3文件

system('locate -i '+ARGV[0]+' >/locate.0');
iter=""
no=0
ARGV.each {
   |iter|
   system('grep -i </locate.'+no.to_s+' '+iter+' >/locate.'+(1-no).to_s);
   no=1-no
   }
system('grep </locate.'+no.to_s+' .mp3$ >/locate.tmp.m3u');
# system('dcop amarok playlist addMedia /locate.tmp.m3u');
system('xmms /locate.tmp.m3u');

Ruby 窗户上的IRB清晰屏幕

# throw this inside %userprofile%\.irbrc and you're good
def cls
  system('cls')
end