注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

关于Ruby的小练习  

2012-03-03 00:00:11|  分类: Ruby |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#!/usr/bin/ruby -w
#author @hjzheng

class DS
def get_cpu_info(id)
"get cpu info #{id}"
end

def get_cpu_price(id)
"get cpu price #{id}"
end

def get_disk_info(id)
"get disk info #{id}"
end

def get_disk_price(id)
"get disk price #{id}"
end

def get_keyboard_info(id)
"get keyboard info #{id}"
end

def get_keyboard_price(id)
"get keyboard price #{id}"
end
end

class Computer
def initialize(c_id, c_ds)
@id = c_id
@data_source = c_ds
end

def cpu
component :cpu
end

def disk
component :disk
end

def keyboard
component :keyboard
end

def component(name)
info = @data_source.send("get_#{name}_info", @id)
price = @data_source.send("get_#{name}_info", @id)
result = "info: #{info} \n price: #{price}"
result
end
end


ds = DS.new
computer = Computer.new("1", ds)
puts computer.cpu
puts computer.disk
puts computer.keyboard

computer2 = Computer.new("2", ds)
puts computer2.cpu
puts computer2.disk
puts computer2.keyboard

class Computer1
def initialize(c_id, c_ds)
@id = c_id
@data_source = c_ds
end

def self.define_component(name)
define_method(name){
info = @data_source.send("get_#{name}_info", @id)
price = @data_source.send("get_#{name}_info", @id)
result = "info: #{info} \n price: #{price}"
result
}
end

define_component :cpu
define_component :disk
define_component :keyboard

end

computer = Computer1.new("1", ds)
puts computer.cpu
puts computer.disk
puts computer.keyboard

computer2 = Computer1.new("2", ds)
puts computer2.cpu
puts computer2.disk
puts computer2.keyboard

class Computer2
def initialize(c_id, c_ds)
@id = c_id
@data_source = c_ds
c_ds.methods.grep(/^get_(.*)_info$/) {Computer2.define_component $1}
end

def self.define_component(name)
define_method(name){
info = @data_source.send("get_#{name}_info", @id)
price = @data_source.send("get_#{name}_info", @id)
result = "info: #{info} \n price: #{price}"
result
}
end

end


ds.methods.grep(/^get_(.*)_info$/) {puts $1}
computer = Computer2.new("1", ds)
puts computer.cpu
puts computer.disk
puts computer.keyboard

computer2 = Computer2.new("2", ds)
puts computer2.cpu
puts computer2.disk
puts computer2.keyboard

#method_missing

class Computer3
def initialize(c_id, c_ds)
@id = c_id
@data_source = c_ds
end

def method_missing(name, *args)
info = @data_source.send("get_#{name}_info", @id)
price = @data_source.send("get_#{name}_info", @id)
result = "info: #{info} \n price: #{price}"
result
end
end

computer = Computer3.new("1", ds)
puts computer.cpu
puts computer.disk
puts computer.keyboard

computer2 = Computer3.new("2", ds)
puts computer2.cpu
puts computer2.disk
puts computer2.keyboard

参考资料: Ruby元编程
  评论这张
 
阅读(441)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017