Ruby主要方法
方法定义 | |||
def hello(name) ... end |
|||
函数名 | 参数 | 作用 | 备注 |
print(),puts(),p() | 任意 | 输出 | puts()带换行,p()不分析内容,带换行 插值 #{} |
# =begin ....=end |
注释 | 单行 # 多行 =begin ....=end |
|
类相关 | |||
class-name.include?() | 是否包含某模块 | ||
ancestors() | 所有祖先 | ||
superclass() | 直接的母类 | ||
运算符相关 | |||
str.inspect() | 返回带格式的字符串 "\"str"\" 相当于Swift的String description |
||
控制语句 | |||
if a > 5 (then) ... else ... end |
if | then可省略 | |
while a > 5 (do) ... end |
while | do可省略 | |
array.each do |var| ... end |
|||
"sada".each_char { |chr| ... } |
|||
数据类型 | |||
[a,b,c,d] | 数组 | ||
{1: a, 2: b, 3: c} | hash | hash量和:之间不要有空格 myHash = {:a => 1, :b => 2, :c => 3}等价 访问用myHash[:a] |
|
/abcd/ | 正规表现 | 最后加i(/abcd/i) 不区分大小写 | |
型变换 | |||
.to_i | |||
.to_s | |||
.to_f | |||
命令行 | |||
ARGV[] | 文件名后第一个为 ARGV[0] | ||
文件操作 | |||
File.open | |||
File.read | File.read("path")读取全部 | ||
File.close | |||
常数 | 以大写字母开头 | ||
变数 | 以小写字母开头 | 本地变量 a 全局变量 $a 实例变量 @a 类变量 @@a |
|
a, b, *c = 1, 2, 3,4,5,6,7 | a = 1, b = 2 c = [3, 4, 5, 6, 7] |
||