ruby
lantianjialiang
这个作者很懒,什么都没留下…
展开
-
Ruby中的Symbol
A symbol is an identifier whose first character is a colon ( : ), so :this is a symbol and so is :that. symbol是以:开头的。 A symbol is not: it is not a string, it is not a constant and it is not a variab原创 2016-08-18 11:12:48 · 382 阅读 · 0 评论 -
Ruby中的 class method, instance method, singleton method/class
singleton method/class 从效果来说,两者没有什么区别# class method10.times{ |i| print("=")}puts("class/instance method")class MyClass def MyClass.classMethod puts( "This is a class method" ) end原创 2016-08-18 11:17:35 · 956 阅读 · 0 评论 -
Ruby的Block
一般来说,用花括号括起来的代码段,称之为blockSome codes segmetn in “The Book for Ruby” can’t run successful in Ruby 2.3 like below code: a = “hello world”.split(//).each{ |x| newstr << x.capitalize } will throw undefi原创 2016-08-18 11:28:16 · 380 阅读 · 0 评论 -
Ruby meta programming 4(eval/class_eval/module_eval/instance_eval)
之所以有不同的eval方法,只是因为在不同的上下文中;你也可以使用eval+bind参数完全替换class_eval/module_eval/instance_eval的# "#{exp}" evaluates it as a string and not as an expression, whereas eval( exp ) evaluates a string as an expressi原创 2016-08-18 13:04:54 · 380 阅读 · 0 评论 -
Ruby meta programming 3(closure)
# closure10.times{ |i| print("=")}puts("closure")x = "hello world"ablock = Proc.new { puts( x ) }def aMethod( aBlockArg ) x = "goodbye" aBlockArg.callendputs( x )ablock.call#this will out原创 2016-08-18 12:55:24 · 460 阅读 · 0 评论 -
Ruby meta programming 2(define_method/attr_accessor)
下面是使用四种方式来完成同一个类的方法。 第一种是直接定义get和set方法; 第二种是通过define_method来动态的定义; 第三种是通过attr_accessor来动态的定义; 第四种也是通过attr_accessor来动态的定义,只是去除了冗余;10.times{ |i| print("=")}puts("Writing Code That Writes Code")clas转载 2016-08-18 12:47:56 · 492 阅读 · 0 评论 -
Ruby 中的各种变量(local/instance/class/global variable and assignment method)
从注释中可以看出每段代码中使用的变量类型# local variable10.times{ |i| print("=")}puts("local variable")1.times do a = 1 b = "a" puts "local variables in the block: #{local_variables.join ", "}"endputs "no local原创 2016-08-18 11:06:17 · 583 阅读 · 0 评论