https://www.udacity.com

http://www.codecademy.com/

http://rubymonk.com/

ruby编程最佳建议

http://ruby-china.org/wiki/coding-style

rubyconf 视频

http://ruby-china.org/topics/15749

可以自己定义类方法, 改变表名, self.table_name=


class Keyword < ActiveRecord::Base
  establish_connection $dbconfig['production_qihu']

  @product = 'hotel'
  @date = Time.now.strftime("%Y%02m")


  def self.product(product)
    @product = product if product and !product.empty?
    self.table_name = "#{@product}_#{@date}_keywords"
    return self
  end

  def self.date(date)
    @date = date if date and date.size == 6
    self.table_name = "#{@product}_#{@date}_keywords"
    return self
  end
end

使用实例

puts Keyword.product(table_product).date(table_date).take

module Kkk
  def table_name
    "#{@name1}_#{@name2}_#{@name3}"
  end
end

class Abc < ActiveRecord::Base
  @name1 = 'aaa'
  @name2 = 'bbb'
  @name3 = 'ccc'
  extend Kkk
end

class Xyz < ActiveRecord::Base
  @name1 = 'ddd'
  @name2 = 'bbb'
  @name3 = 'ccc'
  extend Kkk
end

p Abc.take
p Xyz.take

response = HTTParty.get("http://www.baidu.com")
puts response.headers["set-cookie"][0]

OptionParser.new do |o|
  o.banner = "USAGE: #{$0} [options] [url]"

  o.on("-f",
       "--format [FORMAT]",
       "Output format to use instead of pretty-print ruby: " +
       "plain, json or xml") do |f|
    opts[:output_format] = f.downcase.to_sym
  end

  o.on("-a",
       "--action [ACTION]",
       "HTTP action: get (default), post, put, delete, head, or options") do |a|
    opts[:action] = a.downcase.to_sym
  end

  o.on("-d",
       "--data [BODY]",
       "Data to put in request body (prefix with '@' for file)") do |d|
    if d =~ /^@/
      opts[:body] = open(d[1..-1]).read
    else
      opts[:body] = d
    end
  end

  o.on("-H", "--header [NAME:VALUE]", "Additional HTTP headers in NAME:VALUE form") do |h|
    abort "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
    name, value = h.split(':')
    opts[:headers][name.strip] = value.strip
  end

  o.on("-v", "--verbose", "If set, print verbose output") do |v|
    opts[:verbose] = true
  end

  o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
    abort "Invalid credentials format. Must be user:password" unless u =~ /.*:.+/
    user, password = u.split(':')
    opts[:basic_auth] = { :username => user, :password => password }
  end

  o.on("-r", "--response-code", "Command fails if response code >= 400") do
    opts[:response_code] = true
  end

  o.on("-h", "--help", "Show help documentation") do |h|
    puts o
    exit
  end
end.parse!

https://github.com/jnunemaker/httparty/blob/master/bin/httparty