learn X in Y minutes
最近被 @方应杭 安利了一个网站,learn X in Y minutes,网址如下:learnxinyminutes.com/
网站里提供了一些代码例子可以帮助你快速了解一门语言,我觉得很好用。
这篇文章是我了解 Ruby 的一些代码例子,记录一下供以后翻看。
线上环境
有一个好用的线上环境会更方便我们学习,这里我使用的是 replit.com/ 。
新建一个 repl,
之后线上环境会把需要的东西都准备好,我们就只负责快乐地敲代码,然后点 RUN ~
甚至我们写到一个API的时候这个线上环境也自动提示并且给出一些例子,
nice!!!
语法简要介绍
一切皆对象
p 3.class # p 即为输出
# Integer
p 'Hello'.class
# String
p nil.class # 类似于 null
# NilClass
p true.class
# TrueClass
p false.class
# FalseClass
# 甚至方法也是对象
p 'Hello'.method(:include?).class
# Method
字符串插值
str = 'hello'
p "say #{str}!"
# "say hello!"
一些基本的运算符
和其他语言类似,+ - * / ** %
基本操作,& | ^
位操作符。
值得注意的是,这些操作符都是对象的方法。
p 100.methods
=begin
[……省略…… :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :%, :&, :*, :+, :-, :/, :<, :>, :ord, :^, ……省略……]
=end
p 100.+(100)
# 200
p 100.*(100)
# 10000
p 100.methods.include?(:/) # 这句代码意思是 100 的方法里是否存在除法
# true
比较运算符也很符合直觉,就不一一列举了:
p 1 == '1'
# false
p 2 == 2
# true
p 1 != '1'
# true
有一个值得一提的点,就是组合比较运算符(Combined comparison operator):两数比较,如果第一个数大则返回 1 ,如果第一个数小则返回 -1 ,如果两数相等则返回 0 。
1 <=> 10
# -1
10 <=> 1
# 1
1 <=> 1
# 0
另外,语言中都有 falsey 值的概念,即某个值可以被转化为 false ,Ruby 中 falsey 值只有两个,false
和 nil
p !!nil
# false
p !!false
# false
p !!0
# true
p !!""
# true
数组
可以包含不同的数据类型,有一些快捷的方法
arr = [1, 'hello', false]
p arr
# [1, "hello", false]
arr.first
arr.last
arr.reverse
arr.reverse! # 使用 ! 会修改原属组
arr.push 1
arr << 1 # 相当于 push
甚至 []
也是 Array 的方法:
arr = [1, 'hello', false]
p arr[1]
# 'hello'
p arr.[] 1
# 'hello'
p arr.methods.include?(:[])
# true
解构赋值
a, b, c = [1, 2, 3]
p "#{a}, #{b}, #{c}"
# "1, 2, 3"
符号(Symbol)
以冒号开头的值,
status = :status
status1 = :status
p status.class
# Symbol
p status
# :status
p status == status1
# true
可以和字符串互相转换
status = :status
pending = 'pending'
p status.to_s
# "status"
p pending.to_sym
# :pending
哈希(Hash)
key 如果是字符串,则必须带引号
hash = { 'name' => 'leyili', 'age' => 18 }
p hash
# {"name"=>"leyili", "age"=>18}
hash.keys
# ["name", "number"]
p hash.key? 'name'
# true
p hash.value? 'leyili'
# true
如果 key 是符号,则可简写:
hash = { name: 'leyili', age: 5 }
p hash
# {:name=>"leyili", :age=>5}
条件判断
和其他语言类似,这里只列举出来关键字。
if true
'if statement'
elsif false
'else if, optional'
else
'else, also optional'
end
case xxx
when yyy
puts yyy
when xxx
puts xxx
else
puts xxx
end
另外值得注意的关键字,if
unless
hash = { name: 'leyili', age: 18 }
p 'Hello!' if hash.key? :name
# "Hello!"
p 'nothing' unless hash.key? :nothing
# "nothing"
循环
Ruby 中 for
循环不太常见,使用最多的当属 each
。
遍历数组
even_numbers = []
(1..10).each do |num|
even_numbers << num if num.even?
end
p even_numbers
# [2, 4, 6, 8, 10]
上述代码的主体部分去掉 do
和 end
关键字也可以使用花括号包起来。
(1..10).each {
|num| even_numbers << num if num.even?
}
遍历散列
hash = {
'name' => '张三',
'age' => 20
}
hash.each do |key, value|
p "#{key}:#{value}"
end
# "name:张三"
# "age:20"
方法(Method)
常规操作如下:
def sum(x, y)
x + y
end
p sum(3, 4)
# 7
p sum 3, 4
# 7
p sum sum(3, 4), 5
# 12
参数解构
def sum(first, second, third)
p "#{first}, #{second}, #{third}"
end
params = [1,2,3]
sum(*params)
# "1, 2, 3"
def sum(first, second, third, *other)
p "#{first}, #{second}, #{third}, and more #{other.length}"
end
params = [1,2,3,4,5]
sum(*params)
# 1, 2, 3, and more 2"
类(Class)
基本例子
class Human
# new时初始化
def initialize(name, age=0)
@name = name
@age = age
end
# 设置属性可读
attr_reader :name
attr_reader :age
end
leyili = Human.new('leyili', 18)
p leyili
# #<Human:0x000000000237b6b0 @name="leyili", @age=18>
p "name, #{leyili.name}, age, #{leyili.age}"
# "name, leyili, age, 18"
继承
class Doctor < Human
def injection
'打针'
end
end
leyili = Doctor.new('leyili', 18)
p leyili.injection
# "打针"