您的位置:首页 > 编程语言 > Ruby

《Learn Ruby the Hard Way》

2016-01-20 09:51 519 查看

系统函数

exit(0)
:直接退出程序,并将值返回给操作系统,返回0一般表示正常

输入、输出操作

$stdin.gets
:从console读取一行

$stdin.gets.chomp
:从console读取一行,并删除首尾空白字符

<FileObj>.gets.chomp
:从打开的文件对象读取一行,并删除首尾空白字符

print <s>
:将s输出至console,后面无回车换行符

puts <s>
:将s输出至console,后面有回车换行符

字符串操作:

<StringObj>.length
:返回字符串长度

数组操作:

<ArrayObj>.length
:返回数组长度

<ArrayObj>.shift
:移除数组首元素,并返回该元素

<ArrayObj>.pop
:移除数组尾元素,并返回该元素

<ArrayObj>.sort
:对数据进行排序,并返回排序后数组

文件操作:

open(file_name)
:打开文件

open(file_name, 'w')
:以写入模式打开文件

<FileObj>.close
:关闭文件

<FileObj>.read
:读取文件全部内容

<FileObj>.readline
:读取文件一行内容

<FileObj>.write(s)
:将字符串s写入文件

<FileObj>.seek(n)
:转到位置n处

File.exist?(file_name)
:检测试文件file_name是否存在。

定义函数、调用函数:

def <function_name>[(<args>)]
<statements>
end


<function_name>[(<args>)]


参数形式:

*args
: 表示数组

args
: 表示单个参数

条件语句:

if <condition>
<statements>
end


if <condition>
<statements>
elsif <condition>
<statements>
else
<statements>
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: