Ruby 红宝石每个都有索引

@something.each_with_index do |thing,index|
    puts index
end

Ruby 加载特定版本的gem

# require rubygems first
require 'rubygems'

# e.g.
# gem 'rspec', '=1.1.12'
gem '<gem>', '=<version>'

Ruby 关注Ruby中的粉丝

require 'twitter'
# Check out the twitter gem docs for using oauth
httpauth = Twitter::HTTPAuth.new("username", "password")
base = Twitter::Base.new(httpauth)
to_follow_ids = base.follower_ids - base.friend_ids
unavailable_count = 0
to_follow_ids.each do |tfid|
  begin
    base.friendship_create(tfid, true)
  rescue Twitter::General
    # Twitter::General is raised for 403 errors
    # Which occur when you're trying to follow someone who's been banned by twitter
    base.block(tfid)
  rescue Twitter::Unavailable 
    # Wait and try again if twitter's telling you to wait
    sleep 5
    if unavailable_count < 3
      retry
      unavailable_count += 1
    end
  end
end

Ruby 单身人士课程

# How to define and use a singleton class
# - modifies the class's class
# - instance methods in the singleton class are class methods externally

class TheClass
  class << self
    def hello
      puts "hi"
    end
  end
end

# invoke a class method
TheClass.hello            # hi

Ruby Ruby:州缩写

STATE_ABBR = {
  'AL' => 'Alabama',
  'AK' => 'Alaska',
  'AZ' => 'Arizona',
  'AR' => 'Arkansas',
  'CA' => 'California',
  'CO' => 'Colorado',
  'CT' => 'Connecticut',
  'DE' => 'Delaware',
  'FL' => 'Florida',
  'GA' => 'Georgia',
  'HI' => 'Hawaii',
  'ID' => 'Idaho',
  'IL' => 'Illinois',
  'IN' => 'Indiana',
  'IA' => 'Iowa',
  'KS' => 'Kansas',
  'KY' => 'Kentucky',
  'LA' => 'Louisiana',
  'ME' => 'Maine',
  'MD' => 'Maryland',
  'MA' => 'Massachusetts',
  'MI' => 'Michigan',
  'MN' => 'Minnesota',
  'MS' => 'Mississippi',
  'MO' => 'Missouri',
  'MT' => 'Montana',
  'NE' => 'Nebraska',
  'NV' => 'Nevada',
  'NH' => 'New Hampshire',
  'NJ' => 'New Jersey',
  'NM' => 'New Mexico',
  'NY' => 'New York',
  'NC' => 'North Carolina',
  'ND' => 'North Dakota',
  'OH' => 'Ohio',
  'OK' => 'Oklahoma',
  'OR' => 'Oregon',
  'PA' => 'Pennsylvania',
  'RI' => 'Rhode Island',
  'SC' => 'South Carolina',
  'SD' => 'South Dakota',
  'TN' => 'Tennessee',
  'TX' => 'Texas',
  'UT' => 'Utah',
  'VT' => 'Vermont',
  'VA' => 'Virginia',
  'WA' => 'Washington',
  'WV' => 'West Virginia',
  'WI' => 'Wisconsin',
  'WY' => 'Wyoming'
  }.sort

Ruby Rails - 使用Gmail的SMTP发送电子邮件

ActionMailer::Base.smtp_settings = {
    :enable_starttls_auto => true,  #this is the important shit!
    :address        => 'smtp.gmail.com',
    :port           => 587,
    :domain         => 'your.domain.com',
    :authentication => :plain,
    :user_name      => 'login@your.domain.com',
    :password       => 'assword'
  }

Ruby Ruby:搜索Spotify API

=begin
http://developer.spotify.com/en/metadata-api/overview/

Requests: http://ws.spotify.com/service/version/method[.format]?parameters

http://ws.spotify.com/search/1/track.json?q=kaizers+orchestra

Track Lookup
http://ws.spotify.com/lookup/1/.json?uri=spotify:track:6NmXV4o6bmp704aPGyTVVG

Artist lookup
http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ
http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ&extras=album
http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ&extras=albumdetail

Album lookup
http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4
http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4&extras=track
http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4&extras=trackdetail

Search Example
http://ws.spotify.com/search/1/artist?q=Bj%C3%B6rk.

=end

class SpotifyAPIRequest
  require 'open-uri'

  def initialize
    
  end
  
  def lookup( keyword )
    open("http://ws.spotify.com/search/1/track.json?q=" + keyword){
      |f|
      #Get the Full response which should be the full HTML
      @req = f.read
      #Find the first place where "No match is found", if nothing is found, it will return 'nil'
      puts @req
      
=begin 
      @txt = @req.index("No match")
      puts @txt

      if @txt.nil?
        puts "Domain " + word + ".com is available"
      else
        puts "Domain " + word + ".com is taken"
      end
=end
    }    
  end
end

spotify = SpotifyAPIRequest.new
spotify.lookup("beethoven")

Ruby 将丢失的文件添加到svn

`svn status | grep '?'`.gsub(/\?[^\w]*/,"").split("\n").each { |f| `svn add #{f}`}

Ruby 在quicksilver中设置adium away消息

using terms from application "Quicksilver"
	on process text ThisClipping

		tell application "Adium"
			set my status message to ThisClipping
			set my status type to away
		end tell

	end process text
end using terms from

Ruby ruby会话到期

before_filter :session_expire

private

def session_expire
  if session[:user]
    session_length = 60*10 #10 Minutes for logged in users
    expire_time = session[:expire_time] || Time.now + 10
    if expire_time < Time.now
      reset_session
      flash[:notice] = "You have been logged out due to an extended period of inactivity"
      redirect_to :controller => 'welcome' , :action => 'index'
    else
      session[:expire_time] = Time.now + session_length
    end
  end
end