ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年2月13日 星期五
const_defined? & const_get, class_eval, instance_methods
Module api
#可以檢視有沒有這個常數
Object.const_defined?('BACKOFFICE_IMAGE_HOST')
#get 這個常數
Object.const_get('BACKOFFICE_IMAGE_HOST')
#也可以 set
Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
#有instance_methods 這種好東西
>> User.instance_methods.find_all{|m| m =~/perf/}
=> ["year_to_date_perf", "perf", "user_overall_perf", "today_perf", "one_week_perf", "one_month_perf", "three_month_perf", "signals_with_distinct_target_by_perf", "performance", "six_month_perf", "performance=", "one_year_perf", "month_to_date_perf"]
#module_eval
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
Thing.module_eval("invalid code", "dummy", 123)
#define_method, 動態生method的好物
PERFORMACE_KEY_INTEGER = [
:total, :rank, :success_num, :success_total
]
PERFORMACE_KEY_INTEGER.flatten.each do |m|
define_method(m, lambda {
perf = performance
perf ? perf[m].to_i : nil
})
end
#included, class_eval
module ForumMixin
def self.included(klass)
klass.class_eval do
before_filter :require_admin, :only => [:delete]
before_filter :require_login, :only => [:create]
end
end
end
#method_defined? 也是好物
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言