第一天练习node
puts "Pllease input a number between 0-99" a = rand(100) b = gets() c = b.to_i() while c != a if c < a puts "small" else puts "big" end puts "input again:" b = gets() c = b.to_i() end puts "Bingo"
次日练习编程
散列->数组:keys,values, to_a数组
练习1,ruby
a = (1..16).to_a数据结构
使用each并发
n = 1 a.each do |i| if n%4 != 0 print i print " " else puts i end n = n + 1 end
使用each_slice 框架
a.each_slice(4){|i| p i}
练习2函数
class Tree attr_accessor :children, :node_name def initialize(trees) @node_name = trees.keys[0] @children = [] if trees[@node_name] != nil @children = trees[@node_name].map {|k,v| Tree.new({k=>v})} end end def visit_all(&block) visit &block children.each {|c| c.visit_all &block} end def visit(&block) block.call self end end ruby_tree = Tree.new ({'grandpa' => { 'dad' => {'child 1' => {}, 'child 2' => {} }, 'uncle'=> {'child 3' => {}, 'child 4' => {} } } }) puts "Visiting a node" ruby_tree.visit {|node| puts node.node_name} puts puts "Visiting entire tree" ruby_tree.visit_all {|node| puts node.node_name}
map的使用见http://chrisholtz.com/blog/lets-make-a-ruby-hash-map-method-that-returns-a-hash-instead-of-an-array/学习
练习3ui
File.open("tree2.rb") do |file| file.each_line do |line| if line =~ /tree/ p "#{file.lineno}: #{line}" end end end
第三天练习
module ActsAsCsv def self.included(base) base.extend ClassMethods end module ClassMethods def acts_as_csv include InstanceMethods end end module InstanceMethods def read @csv_contents = [] file = File.new(self.class.to_s.downcase + '.txt') @headers = file.gets.chomp.split(', ') file.each do |row| @csv_contents << row.chomp.split(', ') end end attr_accessor :headers, :csv_contents def initialize read end def each len = @csv_contents.size i = 0 while i < len yield CsvRow.new(@csv_contents[i]) i = i + 1 end end end end class RubyCsv include ActsAsCsv acts_as_csv end class CsvRow attr_accessor :contents def initialize(contents) @contents = contents[0].split(',') #contents是一个相似["name, age, phone"]的字符串,因此分割它为数组["name", "age", "phone"] end def method_missing name, *args numbers = {"one"=>1, "two"=>2, "three"=>3, "four"=>4} col = numbers[name.to_s].to_i - 1 @contents[col] end def to_ary @contents end end m = RubyCsv.new puts m.csv_contents.inspect puts m.each {|row| puts row} puts m.each {|row| puts row.two}
值得注意的是,若是没有定义to_ary而定义的是to_s
def to_s @contents.join(", ") end
则
m.each {|row| puts row}
一句会出现“Can't convert CsvRow to Array(CsvRow#to_ary gives String)”的错误,提示给定String的状况下未定义CsvRow。
但若是去掉to_ary跟method_missing的定义,只定义to_s,则能够正常输出。
这是由于:puts默认调用对象的to_ary函数,找不到时才会试着调用to_s。因此只定义to_s的状况下会正常输出。而若是在定义了上述method_missing的状况下再定义to_s,因为优先寻找的是to_ary,它未定义,因此会用method_missing方法来代替它,但这里我们定义method_missing返回值为String(即@contents[col]),而to_ary的返回值要求是数组,这就是错误“Can't convert CsvRow to Array(CsvRow#to_ary gives String)”出现的缘由。
为了验证这一点,定义to_s函数跟空的method_missing函数,
class CsvRow attr_accessor :contents def initialize(contents) @contents = contents[0].split(',') end def method_missing name, *args end def to_s @contents.join(', ') end end
发现能正常调用to_s打印,说明空的method_missing不会阻止puts继续寻找to_s函数,或者说method_missing返回值为空的状况下,puts会认为to_ary没有找到,进而继续找to_s。
而若是定义
def method_missing name, *args a = ["b","c"] end def to_s @contents.join(', ') end
则会打印
b c
这是由于method_missing的返回值为数组,知足to_ary返回值要为数组的要求,从而puts直接调用method_missing做为to_ary,再也不寻找to_s。
puts调用to_ary的缘由见http://stackoverflow.com/questions/8960685/ruby-why-does-puts-call-to-ary,这个网页还有define_method的几行代码,也值得看。
一些点:
method_missing:能力太强大,直接会覆盖诊断信息,不利于错误调试。相关链接:define_method。
method_missing建议:慎用,上面那个puts的错误就是由于这个。另外一方面,有时候能用它干一些脑洞大开的事情,如XML框架builder容许用户经过 method_missing 方法定义自定义标签,以提供更加美观的语法。