分类 ruby 中的文章

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 匹配……

阅读全文

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会重新打开当前……

阅读全文

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, method finding class User def panels @panels ||= ["Profile", "Products"]……

阅读全文

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 = User.new("user_1", 18) p user.panels puts……

阅读全文

Ruby7 OOP

Everything is Object 一切皆对象 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: 实例变量,成员……

阅读全文

Ruby6 细节补充

代码规范 使用UTF-8编码 使用空格缩进,不使用tab, 1 tab = 2 spaces 不需要使用分号(;)和反斜杠()连接代码 Demo # basic types a = 1 b = "hello world" # one line c = ["pear", "cat",……

阅读全文

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 #……

阅读全文

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……

阅读全文

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的反向断言 unless false "ok" end # => "ok" if/unless a = 1 if a != 1 #如果a不是1 则a复……

阅读全文

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(三个点不包括1……

阅读全文