ruby to_s_ex
Ruby to_s方法 (Ruby to_s method)
In any program code, you accommodate various data types because there are various operations which work on specific data types only. You generally store the value of a data type into the same data type variable but this case is not possible every time. Your data may come from various external sources like Keyboard and database. So, to perform specific operations on them, you need to convert them in specific type first.
在任何程序代码中,您都可以容纳各种数据类型,因为有多种操作仅适用于特定数据类型。 通常,您将数据类型的值存储到相同的数据类型变量中,但是这种情况并非每次都可行。 您的数据可能来自各种外部来源,例如键盘和数据库。 因此,要对它们执行特定操作,您需要首先将它们转换为特定类型。
When you take input from the keyboard, the input is of string type. If you want to perform some mathematical calculations on it, then you have to convert it into an integer with the help of .to_i method.
从键盘上获取输入时,输入为字符串类型。 如果要对其执行一些数学计算,则必须借助.to_i方法将其转换为整数。
Similarly, if you want to convert any type value into a string type, you can take help from .to_s method.
同样,如果要将任何类型的值转换为字符串类型,可以从.to_s方法获得帮助。
In case of the objects belonging to the user-defined class, .to_s method prints the hexadecimal code associated with that instance.
如果对象属于用户定义的类,则.to_s方法将输出与该实例关联的十六进制代码。
.to_s method can be called in the Ruby program with the help of the following syntax:
可以使用以下语法在Ruby程序中调用.to_s方法 :
(Object_name).to_s
Now, let us understand the implementation of .to_s method in a Ruby code with the help of examples.
现在,让我们借助示例来了解Ruby代码中.to_s方法的实现。
Example 1:
范例1:
=begin
Ruby program to show the implementation
of to_s method.
=end
puts "Enter any string"
str = gets.chomp
for k in 1..5
str.concat((k*10).to_s)
end
puts "The final string is #{str}"
Output
输出量
Enter any string
Tina
The final string is Tina1020304050
Explanation:
说明:
In the above code, you can analyze that we are concatenating the loop variable with the string. The loop variable is of integer type and can't be concatenated directly, that's why firstly; we have to convert it into a string with the help of .to_s method and then concatenating it. At last, we are printing the value of the resultant string.
在上面的代码中,您可以分析我们是否将循环变量与字符串连接在一起。 循环变量是整数类型,不能直接连接,这就是为什么; 我们必须借助.to_s方法将其转换为字符串,然后将其串联。 最后,我们将打印结果字符串的值。
Example 2:
范例2:
=begin
Ruby program to show the implementation of
to_s method.
=end
class Example
def initialize
end
end
class Example1
def initialize
end
end
obj1 = Example.new
obj2 = Example1.new
puts "The hexadecimal code of obj1 : #{obj1.to_s}"
puts "The hexadecimal code of obj2 : #{obj2.to_s}"
Output
输出量
The hexadecimal code of obj1 : #<Example:0x00000000031b12e8>
The hexadecimal code of obj2 : #<Example1:0x00000000031b1270>
Explanation:
说明:
In the above code, we are creating two classes and instantiating them. You can observe that .to_s method is overridden and printing some different types of string. This string is nothing but a hexadecimal code associated with the object with which it is called .to_s method is overridden for the objects of user-defined classes. The hexadecimal code starts with the Class name followed by the address of the object.
在上面的代码中,我们正在创建两个类并实例化它们。 您可以观察到.to_s方法被覆盖,并显示了一些不同类型的字符串。 该字符串不过是与被称为.to_s方法的对象相关联的十六进制代码,对于用户定义的类的对象而言,该十六进制代码将被覆盖。 十六进制代码以类名开头,后跟对象的地址。
ruby to_s_ex