包含标签 Ruby articles

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 old_hello hello def hello "new hello" end puts old_hello puts hello File Read/Write 文件读写 File.read File.readlines File#rewind etc IO.read/write File Read file = File.open("run.log", "r") while line = file.gets puts line end File Write file = File.open("run.log", "a+") file.puts "hello" file.close File.open("run.log", "a+") do……

Continue reading

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 = "world" puts user.name puts user.hello # module"s self module Management def self.track "track" end end class User include Management end # User.track # => error Management.track # class_eval in project # requirement: we need to execute a class method when module included module Manegement def self.included……

Continue reading

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"] end end class Admin < User end puts Admin.ancestors admin = Admin.new p admin.panels # 从下往上查找 在admin中查找 找不到往上找User 然后Object 然后Kernel 然后 BasicObject Method Overwrite 方法覆盖 class……

Continue reading

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 "-" * 30 admin = Admin.new("admin_1", 28) puts admin.name p admin.panels # 查看这个类的父类 p Admin.superclass 常用關鍵字 super关键字 调用父类的同名方法 self關鍵字 指向當前作用域實例 # inheritance class User attr_accessor :name, :age def initialize name,……

Continue reading

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: 实例变量,成员属性,属性(property),使用@定义 # class class User def initialize name, age @name = name @age = age end # getter def name @name end def age @age end end user = User.new("Hello", 18) puts user.name puts user.age # class class User def initialize name, age @name = name @age = age end……

Continue reading

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 without() def hello(name, age = 18) puts "hello #{name}, and age is #{age}" end def hello name, age = 18 puts "hello #{name}, and age is #{age}" end 变量类型 local variables 局部变量:……

Continue reading

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 & Float # number puts 1 / 10 puts 1 / 10.0 puts "-" * 30 #output 0.1 ------------------------------ String # string a = "world" b = %Q{……

Continue reading

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 "hello method start" yield("hello","world") puts "hello method end" end hello {|x,y| puts "i am in block,#{x} #{y}"} #output hello method start i am in block,hello world hello method end # yield with paramter def hello name puts "hello method start" result = "hello " + name yield(result) puts "hello method end" end hello("world"){|x| puts "i am in block,i got #{x}"}……

Continue reading

Ruby3 流程控制

if/else/elsif 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复制为1 b = 2 unless defined?(b) #如果b未定义 那么就定义b赋值为2 ## case case a when 1 1 when /hello/ "hello world" when Array [] else "ok" end ## while a = 0 while a < 100 do p a a += 1 end ruby 没有++和–操作符 Iterators for x in [1, 2, 3] do p x……

Continue reading

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 字符串 a = "hello" #=> hello b = "world" #=> world a.class #=> String a + " " + b #=> hello world "#{a} #{b}" #=> hello world string method "hello world".length #=>11 "hello world".capitalize #=> Hello world "hello world".gsub("world","gril").upcase #=> HELLO GIRL 变量赋值 a = "hello" # => hello a.object_id # => 70353313681980 a.replace("hello2") #……

Continue reading