1Debugging Views in Development
ruby 代码
- "debug" style="margin: 40px 5px 5px 5px;">
- "#" οnclick="Element.toggle('debug_info');return false" style="text-decoration: none; color: #ccc;">Show Debug Info ➲
- "debug_info" style="display : none;">
-
还有一个相关的插件:TextmateFootnotesPlugin
ruby 代码
- class << Dispatcher
Application Error
#{e.class}: #{e.message}
#{e.backtrace.join("\n")} RESPONSE end end ensure reset_application end end end
3 SQL Logging in Rails
ruby 代码
- SQL_LOGGING = true
- SQL_LOG_MAX_LINES = 5000
- SQL_LOG_FILE = File::join(RAILS_ROOT, '/log/sql_log.txt')
- # $sql_log is a global var that will hold the results of the last sql statement executed
- $sql_log = ""
- # permit logging if we are in development environment
- if RAILS_ENV_DEV || RAILS_ENV_TEST
- connection = ActiveRecord::Base.connection
- class << connection
- alias :original_exec :execute
- def execute(sql, *name)
- # try to log sql command but ignore any errors that occur in this block
- # we log before executing, in case the execution raises an error
- begin
- if SQL_LOGGING
- if File::exists?(SQL_LOG_FILE) : lines = IO::readlines(SQL_LOG_FILE)
- else lines = Array.new(); end
- log = File.new(SQL_LOG_FILE, "w+")
- # keep the log to specified max lines
- if lines.length > SQL_LOG_MAX_LINES
- lines.slice!(0..(lines.length-SQL_LOG_MAX_LINES))
- end
- lines << Time.now.strftime("%x %I:%M:%S %p")+": "+sql+"n"
- log.write(lines)
- log.close
- $sql_log = sql
- end # if
- rescue Exception => e
- ;
- end
- # execute original statement
- original_exec(sql, *name)
- end # def execute
- end # class <<
- end # if RAILS_ENV_DEV
- Here's the constant setting code (put in application.rb or something it requires before the code above gets run):
- class CoreERR_RailsEnvironment < StandardError; end
- #RAILS specific constants
- #setup global constants for manipulating states
- RAILS_ENV_VAR = "RAILS_ENV"
- #set ENV to development if explicit or not present
- RAILS_ENV_DEV = (ENV[RAILS_ENV_VAR]=="development" || (not ENV[RAILS_ENV_VAR]))
- RAILS_ENV_LIVETEST = ENV[RAILS_ENV_VAR]=="livetest"
- RAILS_ENV_TEST = ENV[RAILS_ENV_VAR]=="test"
- if (RAILS_ENV_DEV) or (RAILS_ENV_TEST)
- RAILS_ENV_PRODUCTION = false
- else
- RAILS_ENV_PRODUCTION = true
- end
- # check positively: if production environment is implied because test and development are not found
- # but production doesn't show up positively, raise exception
- if RAILS_ENV_PRODUCTION and (RAILS_ENV_PRODUCTION != (ENV[RAILS_ENV_VAR]=="production"))
- raise CoreERR_RailsEnvironment, "Production environment implied but not detected: "+ENV[RAILS_ENV_VAR]
- end
- RAILS_DB_SU = ENV[RAILS_ENV_VAR]+'_su'
- RAILS_ENV_DEV.freeze
- RAILS_ENV_TEST.freeze
- RAILS_ENV_PRODUCTION.freeze
3 Digest MD5 & SHA1
Digest 支援 MD5 和 SHA1 兩種編碼, 你若有儲存密碼的需求就要用到, 一般是用 SHA1.
- MD5 計算
require 'digest/md5' puts Digest::MD5.hexdigest("Hello World!")
- 計算檔案的 MD5, 可以確保檔案未曾被修改
require 'digest/md5' #method 1 puts Digest::MD5.hexdigest(File.read("o.rb")) #method 2 class Digest::MD5 def self.open(path) o = new File.open(path) { |f| buf = "" while f.read(256, buf) o << buf end } o end end puts Digest::MD5.open("o.rb").hexdigest
- SHA1 計算
require 'digest/sha1' puts Digest::SHA1.hexdigest("Hello World!")