道喜技术日记 .^. 天天红玉世界

Rails Ruby MacOSX 。。。创新来自于刻苦的实践和勤奋的思考... www.hhtong.com


JavaScript世界:学习CoffeeScript语言(五)

Gudao Luo2011-06-23 at 23:50发表的

JavaScript世界:学习CoffeeScript语言(四)

Gudao Luo2011-06-20 at 07:19发表的

JavaScript世界:学习CoffeeScript语言(三)

Gudao Luo2011-06-19 at 00:43发表的

JavaScript世界:学习CoffeeScript语言(二)

Gudao Luo2011-06-17 at 00:19发表的

  • 道喜评论
    • 关键词class/constructor/this之前的空格不能多,也不能少。
    • 关键词constructor之前是一个(或者两个)空格或者Tab键;关键词this之前是两个(或者四个)空格或者Tab键;

JavaScript世界:学习CoffeeScript语言(一)

Gudao Luo2011-06-16 at 00:36发表的

代码世界:如何理解Ruby类及其函数?

Gudao Luo2010-02-16 at 18:15发表的

2010-02-15-dir
  • 说明
    • 首先要理解类的函数是什么意思。
    • 其次类没有我们所需要的函数,但是有自己想法。
    • 最后就自己写个相关函数,与大家共享。

代码世界:Ruby/Rails反射技术实例

Gudao Luo2009-07-11 at 06:51发表的

Ruby/Rails反射技术实例
  • 使用正常类方法代码
@blogs = Blog.find(:all)
  • 使用反射技术代码(Ruby语言)
@blogs = Kernel.const_get("Blog").send(:find, :all)
@blogs = Object.const_get("Blog").send(:find, :all)
@blogs = Kernel.eval("Blog").send(:find, :all)
  • 使用反射技术代码(类名称包含”::”)(Ruby语言)
Model = "ActiveRecord::Base".split('::').inject(Object) do |base,item|
base.const_get(item)
end
class Blog < Model; end
  • 使用反射技术代码(Rails框架)
@blogs = "Blog".classify.constantize.send(:find, :all)

代码世界:Ruby语言元编程的方法

Gudao Luo2008-08-06 at 04:31发表的

Ruby语言元编程的方法
使用函数eval
classname = 'Integer'
eval classname
(eval classname).to_s
eval classname + '.methods'
使用函数const_get
classname = 'Integer'
object = Kernel.const_get(classname)
object.methods
类Object的方法send
#Object.send(methodname)
#Object.send(methodname, parameter)
具体实例
class Company
def self.find(id)
id
end
end
company_id = 1
classname = 'Company'
object = Kernel.const_get(classname)
object.send(:find, company_id)
Company.find(company_id)
说明
# 最后两行代码从作用上而言是完全等价的
# 但是其编程实现方法是完全不同的
# 好比数学上的函数与泛函的相似概念