ruby values_at与地图

values_at与地图

gistfile1.rb
require 'benchmark'

n = 1000
Benchmark.bm do |bm|
  keys = (1..10000).to_a
  hash = Hash[keys.zip(keys)]

  bm.report do
    n.times do
      keys.map{|key| hash[key] }
    end
  end

  bm.report do
    n.times do
      hash.values_at(*keys)
    end
  end
end


# ruby-2.0.0-p195
#        user     system      total        real
#   2.010000   0.060000   2.070000 (  2.074412)
#   1.130000   0.050000   1.180000 (  1.191744)

# jruby-1.7.4
#       user     system      total        real
#   1.680000   0.010000   1.690000 (  1.148000)
#   0.450000   0.020000   0.470000 (  0.410000)

ruby JRuby的枚举器#to_enum将实例变量重新绑定到另一个上下文?

JRuby的枚举器#to_enum将实例变量重新绑定到另一个上下文?

buggy_enumerator.rb
class BuggyEnumerator < Enumerator
  def initialize(that)
    @ivar = that
  end
  def each
    return to_enum(:each) unless block_given?
    @ivar.each{|item| yield item }
  end
end


ruby-2.0.0-p195
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3]
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
=> [1, 2, 3]

ruby-1.9.3-p327
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3] 
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
=> [1, 2, 3]

jruby-1.7.4
> BuggyEnumerator.new([1,2,3]).each.to_a
=> [1, 2, 3]
> BuggyEnumerator.new([1,2,3]).each_entry.to_a
NoMethodError: undefined method `each' for nil:NilClass
        from (irb):7:in `each'
        from org/jruby/RubyEnumerable.java:378:in `to_a'
        from (irb):11:in `evaluate'
        from org/jruby/RubyKernel.java:1093:in `eval'
        from org/jruby/RubyKernel.java:1489:in `loop'
        from org/jruby/RubyKernel.java:1254:in `catch'
        from org/jruby/RubyKernel.java:1254:in `catch'
        from …/.rvm/rubies/jruby-1.7.4/bin/irb:13:in `(root)'

ruby If-Modified-Since标题检查

If-Modified-Since标题检查

controller.rb
def show
  @show = Show.find(params[:id])

  if stale?(:etag => @show, :last_modified => @show.updated_at.utc)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @show }
    end
  end
end

ruby gistfile1.rb

gistfile1.rb
def funky_method(arg)
  if arg == 1
    "I return a string"
  else
    ["I", "return", "an", "array"]
  end
end

ruby rails3创建自己的红宝石宝石

rails3创建自己的红宝石宝石

rails3CreateGem.rb
_

ruby rails3列出活动记录表

rails3列出活动记录表

rails3ListTables.rb
// controller
@tables = ActiveRecord::Base.connection.tables.sort

ruby 简单的Web服务器,Webrick

简单的Web服务器,Webrick

server.rb
require 'webrick'
include WEBrick

root = File.expand_path(File.dirname($0))
server = WEBrick::HTTPServer.new :Port => 3000, :DocumentRoot => root
trap 'INT' do server.shutdown end
server.start

ruby rails3 prawn生成pdf

rails3 prawn生成pdf

rails3PrawnPDF.rb
gem 'prawn'

// Recipie Model
def shopping_list_pdf
  shopping_list = ingredients.map do |ingredient|
      [ingredient.name, ingredient.quantity]
  end
  
  pdf = Prawn::Document.new
  pdf.table([[ "Item", "Quantity" ], *shopping_list]) do |t|
    t.header = true
    t.row_colors = [ "aaaaff", "aaffaa", "ffaaaa" ]
    t.row(0).style :background_color => '448844', :text_color => 'ffffff' t.columns(1).align = :right
    end
    pdf.render
end

// Recipie Controller
def shopping_list
  @recipe = Recipe.find(params[:id]) 
  respond_to do |format|
    format.html format.pdf do
      send_data @recipe.shopping_list_pdf,
        content_type: Mime::PDF
    end
  end
end


// Routes
resources :recipes do
  member do
    get "shopping_list"
  end
  resources :ingredients
end

ruby 我通常如何构建我的Rspec模型规范。

我通常如何构建我的Rspec模型规范。

model_spec_structure.rb
require 'spec_helper'

describe Order do
  describe '.responds_to' do
    # attributes
    it { should respond_to(:recipient_name) }
    it { should respond_to(:shipping_to_address) }
    it { should respond_to(:shipping_to_city) }
    it { should respond_to(:shipping_to_state) }
    it { should respond_to(:shipping_to_zip) }
    it { should respond_to(:phone) }
    it { should respond_to(:status) }
    it { should respond_to(:cost) }
    
    # associations
    it { should respond_to(:coupon) }
  end

  describe '.valid?' do
    # check to make sure our factory is valid
    it 'has a valid factory' do
      order = FactoryGirl.build(:order)
      order.should be_valid
    end

    # test validations with shoulda_matchers
    it { should validate_presence_of(:recipient_name) }
    it { should validate_presence_of(:shipping_to_address) }
    ...
  end

  # Now test all methods in the model. For more good info on writing specs:
  # http://blog.carbonfive.com/2010/10/21/rspec-best-practices/

  describe '.create' do
    before do
      @order = FactoryGirl.build(:order)
    end

    it 'sends a notification email to the user' ...
  end
end

ruby huboard的当前数据收集代码

huboard的当前数据收集代码

gistfile1.rb
  class Board
    board_method = self.instance_method(:board)

    def couch
      @couch ||= Huboard::Couch.new :base_url => ENV["CLOUDANT_URL"]
    end

    define_method(:board) do

      api = connection_factory.call
      therepo = api.repos(user, repo)
      theuser = api.users(user)

      couch.users.get_or_create theuser if theuser.type == "User"
      couch.orgs.get_or_create theuser if theuser.type == "Organization"
      couch.repos.get_or_create therepo 

      theboard = board_method.bind(self).call

      #couch.boards.save theboard

      theboard
    end

  end