This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/clear_logs.rb | |
# This snippet simply clears your logs when they are too large. | |
# Large logs mean looooong search in TextMate. You know it :) | |
# Every time you run rails server or rails console it checks log sizes | |
# and clears the logs for you if necessary. | |
if Rails.env.development? | |
MAX_LOG_SIZE = 2.megabytes | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t = DateTime | |
id = t.now.strftime("%Y%m%d%k%M%S%L") # Get current date to the milliseconds | |
id = id.to_i.to_s(36) # will generate somthing like "5i0sp1h4tkc" | |
# Reverse it | |
id.to_i(36) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Put this file in the root of your Rails project, | |
# then run it to output the SQL needed to change all | |
# your tables and columns to the same character set | |
# and collation. | |
# | |
# > ruby character_set_and_collation.rb | |
DATABASE = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************/ | |
基本命令 | |
C-x C-f 打开/新建文件 | |
C-x C-s 保存当前缓冲区 | |
C-x C-w 当前缓冲区另存为 | |
C-x C-v 关闭当前Buffer并打开新文件 | |
C-x i 光标处插入文件 | |
C-x b 切换Buffer | |
C-x C-b 显示Buffer列表 | |
C-x k 关闭当前Buffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
const Delta = 0.0001 | |
func isConverged(d float64) bool { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def try_all(*methods) | |
values = [self] | |
methods.each do |method| | |
value = values.last.try(method) | |
return nil if value.nil? | |
values << value | |
end | |
values.last |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
socket = TCPSocket.open("www.theonion.com", "80") | |
TCPSocket.open("www.theonion.com", 80) do |socket| | |
socket.puts "GET / HTTP/1.0\n\n" | |
puts socket.read | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'kaminari' | |
gem 'ransack' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hash = { 'foo' => 'bar' } | |
# Version 1 | |
hash = Hash[hash.map { |k, v| [k.to_sym, v] }] | |
# Version 2 | |
hash = hash.reduce({}) do |memo, (k, v)| | |
memo.tap { |m| m[k.to_sym] = v } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variant 1 | |
def user_params(type) | |
params.require(type.to_sym).permit(attributes) | |
end | |
# Variant 2 | |
def user_params(type) | |
case type | |
when "user" | |
params.require(:user).permit(user_attributes) |
OlderNewer