Ruby method 学习笔记
2013-10-09 19:24
330 查看
Ruby 关于Method 一、 定义方法 使用关键字 def 方法名称以小写字母或下划线开头,后面跟着字母、数字和下划线 有的方法名后面跟着?,!或= 方法名有?通常意味着方法返回boolean型结果 例: 1.even? # => false 2.even? # => true 1.instance_of?(Fixnum) # => true 方法名后有!,则此方法的接收者将被修改 方法名后加=,通常意味着赋值 Ruby方法的参数可以设定默认值 def cool_dude(arg1="Miles", arg2="Coltrane", arg3="Roach") "#{arg1}, #{arg2}, #{arg3}." end cool_dude # => "Miles, Coltrane, Roach." cool_dude("Bart") # => "Bart, Coltrane, Roach." cool_dude("Bart", "Elwood") # => "Bart, Elwood, Roach." cool_dude("Bart", "Elwood", "Linus") # => "Bart, Elwood, Linus." 二、可变长度的参数 在参数前加*号,用以接收多个参数值或可变参数值 def varargs(arg1, *rest) "arg1=#{arg1}. rest=#{rest.inspect}" end varargs("one") # => arg1=one. rest=[] varargs("one", "two") # => arg1=one. rest=[two] varargs "one", "two", "three" # => arg1=one. rest=[two, three] 使用*号表示一个在方法中不使用,但可能在父类中使用到的参数 class Child < Parent def do_something(*not_used) # our processing super end end 这种情况下,可以不需要写参数名称,只使用*表示参数 class Child < Parent def do_something(*) # our processing super end end 在Ruby1.9中,我们可以把此类参数放在任何位置,如: def split_apart(first, *splat, last) puts "First: #{first.inspect}, splat: #{splat.inspect}, " + "last: #{last.inspect}" end split_apart(1,2) split_apart(1,2,3) split_apart(1,2,3,4) produces: First: 1, splat: [], last: 2 First: 1, splat: [2], last: 3 First: 1, splat: [2, 3], last: 4 三、方法和Block 在方法中使用block通常使用yield def double(p1) yield(p1*2) end double(3) {|val| "I got #{val}" } # => "I got 6" double("tom") {|val| "Then I got #{val}" } # => "Then I got tomtom" 然而,如果方法定义中的最后一个参数以&开头,则,所有相关的block都会转换为Proc对象。 class TaxCalculator def initialize(name, █) @name, @block = name, block end def get_tax(amount) "#@name on #{amount} = #{ @block.call(amount) }" end end tc = TaxCalculator.new("Sales tax") {|amt| amt * 0.075 } tc.get_tax(100) # => "Sales tax on 100 = 7.5" tc.get_tax(250) # => "Sales tax on 250 = 18.75" 四、方法调用 方法的默认接收者为self 在一个类方法或module中的方法,接收者为类或module的名称 五、把参数传给一个方法 六、方法的返回值 每一个调用的方法都会有一个返回值。方法的返回值是最后一个语句执行的结果 def meth_one "one" end meth_one # => "one" def meth_two(arg) case when arg > 0 then "positive" when arg < 0 then "negative" else "zero" end end meth_two(23) # => "positive" meth_two(0) # => "zero" Ruby有一个关于return 的语句 如果return多个值,则方法会返回一个数组 def meth_three 100.times do |num| square = num*num return num, square if square > 1000 end end meth_three # => [32, 1024] num, square = meth_three num # => 32 square # => 1024 七、Splat,将扩张集合到方法调用中 这个是Ruby1.9中的特性 之前我们看到在方法定义的参数中,在参数前加*,可以将多个参数值传给一个参数, 这里正好相反。 调用一个方法时,你可以把任意的集合或列举型对象转变成组合的元素,然后把这些元素传递给单独的参数了。在传递的数组参数前加上*。 def five(a, b, c, d, e) "I was passed #{a} #{b} #{c} #{d} #{e}" end five(1, 2, 3, 4, 5 ) # => "I was passed 1 2 3 4 5" five(1, 2, 3, *['a', 'b']) # => "I was passed 1 2 3 a b" five(*['a', 'b'], 1, 2, 3) # => "I was passed a b 1 2 3" five(*(10..14)) # => "I was passed 10 11 12 13 14" five(*[1,2], 3, *(4..5)) # => "I was passed 1 2 3 4 5" 八、让Blocks更加动态一些 如果方法的最后一个参数值有&,Ruby假定这是一个Proc对象,Ruby会把该Proc 对象转换为一个block,然后在方法中使用。 九、收集Hash变量 一般方法中,参数值对应一定的参数变量,传递参数就需要按照一定的顺序。 在Ruby中可以使用Hash的键值对,来传递参数。 class SongList def search(name, params) # ... end end list.search(:titles, { :genre => "jazz", :duration_less_than => 270 }) 此处,可以将Hash作为一个键=>值参数列表。 list.search(:titles, :genre => 'jazz', :duration_less_than => 270) 在Ruby1.9中,可以写成: list.search(:titles, genre: 'jazz', duration_less_than: 270) 遗留问题: 1. Proc 2. lambda Ps: 再次复习相关Ruby的基础知识,还是很有必要的,有所收获。 看一段和写这篇文章居然用了两天时间,无比惭愧。不管是什么借口,都觉得自己不应该! 好好思考一下吧,自己现在还是不成熟,整天浪费时间!
相关文章推荐
- Ruby 中的 block、proc、lambda
- Ruby函数lambda知识讲解
- 几种实用型Ruby Web开发框架介绍
- 使用 Ruby 解析CSV文件&YAML文件
- 《Ruby语言入门教程v1.0》学习笔记-03
- win7上搭建ruby开发环境
- 周鸿祎:别为蝇头小利放弃重要学习机会
- Ruby for Rails 最佳实践十六
- Ruby for Rails 最佳实践十五
- Ruby for Rails 最佳实践十四
- Ruby for Rails 最佳实践十三
- Ruby for Rails 最佳实践十二
- Ruby for Rails 最佳实践十一
- Ruby for Rails 最佳实践十
- Ruby for Rails 最佳实践九
- Ruby for Rails 最佳实践八
- Ruby for Rails 最佳实践七
- Ruby for Rails 最佳实践Ⅵ
- Ruby for Rails 最佳实践Ⅳ
- Ruby for Rails 最佳实践Ⅴ