軟體工程師都很能理解營運一個網站,完善的 log 系統是不可或缺的。 除了在發生錯誤時能用來除錯以外,也能透過資料分析來當作各項指標, 進行科學的自我改善。
同樣的,一個人生活中,紀錄不也有同樣的好處嗎? 有了紀錄,可以查詢到上次感冒是什麼時候, 新的習慣養成是否成功,生活上有什麼大小改變等等。
def inspect | |
result = instance_variables.each | |
.with_object({}) { |name, h| h[name] = instance_variable_get(name) } | |
.reject { |name, value| value.nil? } | |
.map { |name, value| | |
case value | |
when true, false, Symbol, Numeric, Array, Hash | |
"#{name}=#{value.inspect}" | |
when String | |
if value.length > 80 |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) |
There are many use cases where the ActiveModel::Errors interface is tedious to use, such message formatting and testing. This talk presents these cases, and explains why the limitation can be simply solved by taking an object-oriented approach, which results in the AdequateErrors gem. The gem's implementation is explained, and the talk will showcase examples on how it can make your coding life easier.
foo.with_lock do | |
# insert a | |
bar.with_lock do | |
# Mysql2::Error: Lock wait timeout exceeded | |
end | |
end |
12345678902234567890323456789042345678905234567890623456789072345678908234567890923456789002345678901234567890223456789032345678904234 t.now |
class ActiveRecord::Base | |
def to_s | |
"#<#{self.class.name}:#{self.id}>" | |
end | |
end |
irb(main):016:0> Money.new(1.5) == 0.015.to_money | |
=> true | |
irb(main):017:0> Money.new(1.5).instance_variable_get(:@fractional).to_s | |
=> "1.5" | |
irb(main):018:0> 0.015.to_money.instance_variable_get(:@fractional).to_s | |
=> "2.0" |
# controller | |
@open_id = params[:open].to_i | |
# spec | |
expect(assigns(:open_id)).to eq(@params[:open].to_i) |
require 'nokogiri' | |
require 'open-uri' | |
(1..91).each do |i| | |
f = Fountain.create(issue:i, title:i) | |
end | |
(1..91).each do |i| | |
f = Fountain.find_by(issue:i) | |
i_s = "%02d" % i |