Ruby Rails随机代词密码

# creates random pronouncable passwords
def self.random_password
  c = %w( b c d f g h j k l m n p qu r s t v w x z ) +
      %w( ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr )
  v = %w( a e i o u y )
  f, r = true, ''
  6.times do
    r << ( f ? c[ rand * c.size ] : v[ rand * v.size ] )
    f = !f
  end
  2.times do
    r << ( rand( 9 ) + 1 ).to_s
  end
  r
end

Ruby Rails分手排长队

def break_up_long_line(str, max)
  counter = 0
  new_text = ''
  for i in 0...str.length
    if str[i,1] =~ /\n/
      counter = 0
    else
      counter = counter + 1
    end
    new_text << str[i,1]
    if counter >= max && str[i,1] =~ /\s/
      new_text << "\n"
      counter = 0
    end
  end
  new_text
end

Ruby 协会是不够的

def self.find_with_all_actors(*actors)
  return [] if actors.empty?
  actors = actors.flatten
  find(:all, :readonly => false,
       :joins => "INNER JOIN appearances a ON movies.id = a.movie_id",
       :conditions => "a.actor_id IN (#{actors.map(&:id).join(', ')})",
       :group => "movies.id HAVING COUNT(movies.id) = #{actors.size}")
end

Ruby ActionMailer测试

def setup @user = users(:myfixtureuser) ActionMailer::Base.deliveries = [] end def test_welcome_mail MyMailer.deliver_welcome_email assert !ActionMailer::Base.deliveries.empty? sent = ActionMailer::Base.deliveries.first assert_equal [@user.email], sent.to assert_equal "expected subject", sent.subject assert sent.body =~ /^Welcome to my App/ assert sent.body =~ /^Username: #{@user.login}$/ assert sent.body =~ /^Password: [a-z0-9]{10}$/i end

Ruby 重新迁移

desc "Drop then recreate the dev database, migrate up, and load fixtures" task :remigrate => :environment do return unless %w[development test staging].include? RAILS_ENV ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.drop_table t } Rake::Task[:migrate].invoke Rake::Task["db:fixtures:load"].invoke end

Ruby svn diff

#!/usr/bin/env ruby `svn diff #{ARGV.join(' ')}`.each do |line| puts( if line =~ /^\+(.*)$/ "\e[32m#{$&}\e[0m" elsif line =~ /^-(.*)$/ "\e[31m#{$&}\e[0m" else line end ) end

Ruby 寻找良好的色彩对比度

# Return true if the difference between two colors # matches the W3C recommendations for readability # See http://www.wat-c.org/tools/CCA/1.1/ def colors_diff_ok? c1, c2 cont, bright = find_color_diff c1, c2 (cont > 500) && (bright > 125) # Acceptable diff according to w3c end # Return the contranst and brightness difference between two RGB values def find_color_diff c1, c2 r1, g1, b1 = break_color c1 r2, g2, b2 = break_color c2 cont_diff = (r1-r2).abs+(g1-g2).abs+(b1-b2).abs # Color contrast bright1 = (r1 * 299 + g1 * 587 + b1 * 114) / 1000 bright2 = (r2 * 299 + g2 * 587 + b2 * 114) / 1000 brt_diff = (bright1 - bright2).abs # Color brightness diff [cont_diff, brt_diff] end # Break a color into the R, G and B components def break_color rgb r = (rgb & 0xff0000) >> 16 g = (rgb & 0x00ff00) >> 8 b = rgb & 0x0000ff [r,g,b] end

Ruby 找到一个好的颜色 - 用法

possible_colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0x00ffff, 0xffffff]
good_color = 0 # We can default to black...
possible_colors.each do |c|
  if colors_diff_ok? c, my_color
    good_color = c
    break
  end
end

Ruby 迁移期间创建数据

# e.g. Copy existing data from users table to newly created members table
    userMove = User.find(:all)
    userMove.each do |t|
      Member.create  :first_name => t.forename,
                     :last_name => t.surname,
                     :email => t.email,
                     :password => t.password                   
    end

Ruby 插入多行

def self.insert_multiple_rows(columns, values)
  values_statement = ""
  values.each { |row| values_statement << "(#{columns.length.times}

  connection.execute(<<-EOF)
  INSERT INTO #{table_name} (#{columns.join(",")})
  VALUES
    #{values_statement}
  EOF
end

Now you'll be able to call, for example:

columns = ["username","real_name", "password"]
values  = [
  ["divoxx", "Rodrigo", "foo"],
  ["johng", "John Gary", "yay"]
]
User.insert_multiple_rows(columns, values)