Ruby 简明教程
Ruby - Built-in Functions
由于 Kernel 模块是由 Object 类包含的,因此其方法在 Ruby 程序的任何地方都可用。它们可以在没有接收者(函数形式)的情况下被调用。因此,它们通常被称为函数。
Since the Kernel module is included by Object class, its methods are available everywhere in the Ruby program. They can be called without a receiver (functional form). Therefore, they are often called functions.
Functions for Numbers
这里有一个与数字相关的内置函数列表。它们应该被如下使用 −
Here is a list of Built-in Functions related to number. They should be used as follows −
#!/usr/bin/ruby
num = 12.40
puts num.floor # 12
puts num + 10 # 22.40
puts num.integer? # false as num is a float.
这会产生以下结果 −
This will produce the following result −
12
22.4
false
Conversion Field Specifier
函数 sprintf( fmt[, arg…]) 和 format( fmt[, arg…]) 返回一个字符串,其中 arg 根据 fmt 进行格式化。格式化规范与 C 编程语言中 sprintf 的规范基本相同。fmt 中的转换说明符(% 后跟转换字段说明符)替换为对应参数的格式化字符串。
The function sprintf( fmt[, arg…]) and format( fmt[, arg…]) returns a string in which arg is formatted according to fmt. Formatting specifications are essentially the same as those for sprintf in the C programming language. Conversion specifiers (% followed by conversion field specifier) in fmt are replaced by formatted string of corresponding argument.