Ruby11 拾遗

Agenda Loop Expression File Read/Write Debug Process & Thread Loop while a = 10 while a > 0 puts a a -= 1 end until a = 100 until a == 0 puts a a -= 1 end loop a = 10 loop do break if a 0 !~ 正则匹配 是否匹配不到 匹配到返回fals 匹配不到返回true /666/i !~ "hello world" # => true alias 别名 def hello "hello" end alias …

Ruby10 Class Module Gem 深入

Agenda class_eval & instance_eval method_missing Module as a namespace Gems require vs load $LOAD_PATH class_eval 首先class_eval是只有类才能调用的,Class#class_eval class_eval会重新打开当前类的作用域 # class_eval class User end User.class_eval do attr_accessor :name def hello "hello" end end user = User.new user.name = …

Ruby9 Class & Modules 进阶

Ruby的内部类结构 Array.class # => Class Class.class # => Class superclass 查看父类 Array.superclass # =>Object Object.superclass # =>BasicObject BasicObject.superclass # => nil ancestors 查看当前类的继承链 Array.ancestors # => [Array, Enumerable, Object, Kernel, BasicObject] Method Finding 方法查找 # class structure, …

Ruby8 继承(Inheritance)和模块(Modules)

继承示例 # inheritance class User attr_accessor :name, :age def initialize name, age @name, @age = name, age end def panels # ||= 操作符, 如果变量不存在 那么就赋值 @panels ||= ["Profile", "Products"] end end class Admin < User def panels @panels ||= ["Profile", "Products", "Manage Users", "System Setup"] end end user …

Ruby7 OOP

Everything is Object 一切皆对象 1 2 3 4 5 6 7 a = "hello" a.class # => String b = 3.14 b.class # => Float c = %w[pear cat horse] c.class # => Array Instance Method & Instance Attribute 实例方法,实例属性 Instance Method: 实例方法,成员方法,成员函数 Instance Attribute: 实例变量,成员属性,属性(property),使用@定义 1 2 3 4 5 6 7 8 9 10 11 12 13 …

Ruby6 细节补充

代码规范 使用UTF-8编码 使用空格缩进,不使用tab, 1 tab = 2 spaces 不需要使用分号(;)和反斜杠()连接代码 Demo # basic types a = 1 b = "hello world" # one line c = ["pear", "cat", "dog"] # or multiple lines c2 = [ "pear", "cat", "dog" ] d = { name:"world", age:18 } d2 = { name:"world", age:18 } # source layout, conversion # with or …

Ruby5 其他的奇技淫巧

变量赋值 # 变量交换 a = 1 b = 2 b,a = a,b puts a puts b puts "-" * 30 x = [1, 2, 3] a, b = x #默认会把数组中的值依次赋值给 a ,b puts a puts b puts "-" * 30 x = [1, 2, 3] a, *b = x #这里a会接受第一个元素 b用了*号 表示接受剩下所有的元素 puts a p b #output 2 1 ------------------------------ 1 2 ------------------------------ 1 [2, 3] Fixnum & …

Ruby4 Blocks and Exceptions

Block 代码块 Block是一个参数 匿名参数 Callback 使用do/en或者{}来定义 {puts "hello"} Demo: # block usage def hello puts "hello method start" yield yield puts "hello method end" end hello {puts "i am in block"} #output hello method start i am in block i am in block hello method end # yield with parameter def hello puts …

Ruby3 流程控制

if/else/elsif 1 2 3 4 5 6 7 8 9 a = "hello" b = false if a p a elsif b p b else p "ok" end unless unless相当于if的反向断言 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 unless false "ok" end # => "ok" ## if/unless a = 1 if a != 1 #如果a不是1 则a复制为1 b = 2 unless defined?(b) #如果 …

Ruby1 数据类型,变量

整数类型: 3,222 小数: 3.14 字符串: hello,world 布尔类型: true(TrueClass),false(FalseClass) 数组: [1,2],["hello","hello world"] Hash(字典): {"name"=>"luo","age"=>24},{:name=>"daoyi",:age=>24} Symbol(符号): :a,:hello,:"hello world" Range: 1..10,1...10(三个点不包括10本身) 正则: /hello/i String 字符串 1 2 3 4 5 a = "hello" #=> hello …

Ruby2 Method and Block

Method(Function) 1 2 3 4 5 6 7 8 9 def hi(name) p "hi " + name end hi("666") # => "hi 666" hi "code" #括号省略 => "hi code" def hello name p "hello #{name}" end hello "world" # => "hello world" Method 参数 1 2 3 4 5 6 7 8 9 def hi name="code" p "hi " +name end hi # => "hi code" def hi name,age=32 p …