Ruby 选择

module ActiveRecord
  class Base  
    def self.to_select(conditions = nil)
      find(:all).collect { |x| [x.name,x.id] }
    end
  end
end

class Array
  def to_select
    self.collect { |x| [x.name,x.id] }
  end
end

<%= form_for 'monkey', @monkey do |f| %>

  <%= f.select 'user_id', User.to_select %>

<% end %>

Ruby 图像链接

<%= link_to image_tag("search.gif", :border=>0), :action => 'show', :id => user %>

Ruby nethttp帖子

require 'net/http'

url = URI.parse('http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction')
appid = 'YahooDemo'

context = 'Italian sculptors and painters of the renaissance favored
the Virgin Mary for inspiration'
query = 'madonna'

post_args = {
   'appid' => appid,
   'context' => context,
   'query' => query
}

resp, data = Net::HTTP.post_form(url, post_args)

Ruby 图像标签alt和标题副本

Firefox doesn't display a popup for the alt attribute for images. While this is according to spec, it's slightly annoying to put the same text in both the alt and the title. Put the following in your ApplicationHelper to copy the alt to the title for every image. Please don't tell me how wrong this is. I don't care.

module ApplicationHelper
  def image_tag(location, options)
    options[:title] = options[:alt] if options[:alt]
    super(location, options)
  end
end

Ruby acts_as_recent

module Marmalade
  module ActiveRecord
    module Acts
      module Recent

        def self.included( klass )
          super
          klass.extend(ClassMethods)
        end     

        module ClassMethods

          def acts_as_recent( threshold = 5.days )
            class_inheritable_accessor :recent_threshold
            self.recent_threshold = threshold.is_a?( Fixnum ) ? threshold : 5.days
            send :include, Marmalade::ActiveRecord::Acts::Recent::InstanceMethods 
          end
        end

        module InstanceMethods

          def fresh?
            created_at && created_at > Time.now.utc.ago(recent_threshold)
          end

          def recently_updated?
            updated_at && updated_at > Time.now.utc.ago(recent_threshold)  
          end        

        end

      end
    end
  end
end

ActiveRecord::Base.class_eval { include Marmalade::ActiveRecord::Acts::Recent }

Ruby 浓度工作室12天愚蠢的Ruby技巧

class Array def shuffle n = [] while !empty? n << delete_at(rand(size)) end n end end # Usage >> [1,2,3,4,5].shuffle => [2, 4, 5, 1, 3]

Ruby 克隆dev DB模式以测试Rails中的DB

rake clone_structure_to_test

Ruby 使用或不使用时间格式化日期

def format_date(date, use_time = false)
    if use_time == true
        ampm = date.strftime("%p").downcase
        new_date = date.strftime("%B %d, %Y at %I:%M" + ampm)
    else
        new_date = date.strftime("%B %d, %Y")
    end
end

Ruby Ver sessao

<%= debug(@request.session.instance_variable_get("@data")) %>

Ruby iconv转换

require 'iconv'
module PermalinkFu
  class << self
    attr_accessor :translation_to
    attr_accessor :translation_from
    
    def escape(str)
      s = Iconv.iconv(translation_to, translation_from, str).to_s
      s.gsub!(/\W+/, ' ') # all non-word chars to spaces
      s.strip!            # ohh la la
      s.downcase!         #
      s.gsub!(/\ +/, '-') # spaces to dashes, preferred separator char everywhere
      s
    end
  end
  
  def has_permalink(attr_name, permalink_field = nil)
    permalink_field ||= 'permalink'
    after_validation { |record| record.send("#{permalink_field}=", PermalinkFu.escape(record.send(attr_name).to_s)) if record.send(permalink_field).to_s.empty? }
  end
end

PermalinkFu.translation_to   = 'ascii//ignore//translit'
PermalinkFu.translation_from = 'utf-8'