2018.2.20
更新 2026.7.23 ruby 204 字· 1 分钟 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 …
2018.2.20
更新 2026.7.23 ruby 952 字· 2 分钟 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会重新打开当前类的作用域 # …
2018.2.18
更新 2026.7.23 ruby 647 字· 2 分钟 Ruby的内部类结构 Array.class # => Class Class.class # => Class superclass 查看父类 Array.superclass # =>Object Object.superclass # =>BasicObject BasicObject.superclass # => nil ancestors 查看当 …
2018.2.17
更新 2026.7.23 ruby 228 字· 1 分钟 继承示例 # inheritance class User attr_accessor :name, :age def initialize name, age @name, @age = name, age end def panels # ||= 操作符, 如果变量不存在 那么就赋值 @panels ||= ["Profile", "Products"] …
2018.2.17
更新 2026.7.23 ruby 731 字· 2 分钟 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 …
2018.2.17
更新 2026.7.23 ruby 约 1.1k 字· 3 分钟 代码规范 使用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 = [ …
2018.2.17
更新 2026.7.23 ruby 192 字· 1 分钟 变量赋值 # 变量交换 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用了*号 表示接 …
2018.2.17
更新 2026.7.23 ruby 751 字· 2 分钟 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 …
2018.2.16
更新 2026.7.23 ruby 199 字· 1 分钟 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 …
2018.2.16
更新 2026.7.23 ruby 约 1.1k 字· 3 分钟 整数类型: 3,222 小数: 3.14 字符串: hello,world 布尔类型: true(TrueClass),false(FalseClass) 数组: [1,2],["hello","hello world"] Hash(字典): {"name"=>"luo","age"=>24},{:name=>"daoyi",:age=>24} …
2018.2.16
更新 2026.7.23 ruby 591 字· 2 分钟 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" # => …