Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 添加数字字符串_Ruby_Oop - Fatal编程技术网

Ruby 添加数字字符串

Ruby 添加数字字符串,ruby,oop,Ruby,Oop,这段代码: var1 = 2 var2 = '5' puts var1.to_s + var2 打印25。我想知道为什么。我们将2从整数转换为字符串2,因此原则上,它是2+5=7。但是为什么输出是25?'2'+'5'是'25',因为字符串#+连接了两个字符串 如果要执行算术,两个操作数都必须是数字,而不是字符串。'2'+'5'是'25',因为字符串#+连接了两个字符串 如果要执行算术,两个操作数都必须是数字,而不是字符串。在ruby中,代码 x = '2' + '5' 相当于: x

这段代码:

var1 = 2
var2 = '5'    
puts var1.to_s + var2

打印
25
。我想知道为什么。我们将
2
从整数转换为字符串
2
,因此原则上,它是
2+5=7
。但是为什么输出是
25

'2'+'5'
'25'
,因为
字符串#+
连接了两个字符串


如果要执行算术,两个操作数都必须是数字,而不是字符串。

'2'+'5'
'25'
,因为
字符串#+
连接了两个字符串

如果要执行算术,两个操作数都必须是数字,而不是字符串。

在ruby中,代码

x = '2' + '5'
相当于:

x = '2'.+('5')
试试看——你可以在你的程序里写出来。第二种语法可能看起来很奇怪,但它符合以下格式:

x = someObject.someMethodName(someArgument)
其中:

someObject     => '2'  (a String)
someMethodName => +
someArgument   => '5' (a String)  
是的,
+
实际上是中方法的名称。因此,您可以检查String类,了解名为
+
的方法是如何工作的

语法:

x = '2' + '5'
对于正常的方法语法,被称为语法糖:

x = '2'.+('5')
而且,因为你可以省略括号,你也可以写:

x = '2'.+ '5'
如果您不喜欢String类中名为
+
的方法产生的结果,您可以更改它:

class String
  def +(other)
    return self.to_i + other.to_i
  end
end

puts '2' + '5'

--output:--
7
在ruby中,代码

x = '2' + '5'
相当于:

x = '2'.+('5')
试试看——你可以在你的程序里写出来。第二种语法可能看起来很奇怪,但它符合以下格式:

x = someObject.someMethodName(someArgument)
其中:

someObject     => '2'  (a String)
someMethodName => +
someArgument   => '5' (a String)  
是的,
+
实际上是中方法的名称。因此,您可以检查String类,了解名为
+
的方法是如何工作的

语法:

x = '2' + '5'
对于正常的方法语法,被称为语法糖:

x = '2'.+('5')
而且,因为你可以省略括号,你也可以写:

x = '2'.+ '5'
如果您不喜欢String类中名为
+
的方法产生的结果,您可以更改它:

class String
  def +(other)
    return self.to_i + other.to_i
  end
end

puts '2' + '5'

--output:--
7
让我们玩一玩

2 + 5 = 7 
2.class = Integer
5.class = Integer 

'2' + '5' = '25'
'2'.class = String
'5'.class = String
所以整数
+
是数字加

字符串
+
是串联等价的

。。。但是为什么呢

您需要了解的是,Ruby中的整数是一个对象,字符串也是一个对象。所有对象(在任何OOP语言中)都有可以调用的方法:

 2.public_methods
 => [:%, :&, :*, :+, :-, :/, :<, :>, :^, :|, :~, :-@, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :[], :inspect, :size, :succ, :to_int, :to_s, :to_i, :to_f, :next, :div, :upto,  ....and the list goes on

'2'.public_methods
 => [:include?, :%, :*, :+, :to_c, :count, :unicode_normalize, :unicode_normalize!, :unicode_normalized?, :partition, :unpack, :unpack1, :sum, :next,  ...and the list goes on
实际上您正在调用一个方法
+

2.+(5)
'2'.+('5')
但实际上,在OOP语言中调用方法实际上是向对象“发送”消息:

2.send(:+, 5)
'2'.send(:+, '5')
反正

您需要了解的一件事是,如果您将不同的对象混合在一起,如:

'2' + 5
# => TypeError: no implicit conversion of Integer into String

2 + '5'
# => TypeError: String can't be coerced into Integer
Ruby将通过引发错误来保护您。以防万一你或你的同事在你的代码里做了些蠢事

但是没有什么可以阻止您将一种对象类型转换为另一种对象类型

'2'.to_i + 5 
 #=> 7

 2 + '5'.to_i
 # => 7

 '2' + 5.to_s
 # => '25'
完全面向对象语言(如Ruby)的另一个有趣部分是,您可以使用
+
方法创建自己的对象

class SafePlus
  def initialize(first_value)
    @first_value = first_value
  end

  def +(other_value) 
    @first_value.to_i + other_value.to_i
  end
end

SafePlus.new(2) + '5'
# => 7

SafePlus.new("5") + "107"
# => 112
您还可以重写String类或Integer类的原始实现,以便所有相应对象的行为如下:

module MyIntegerFix
  def +(other)
    super(other.to_i)
  end
end
Integer.prepend(MyIntegerFix)

2 + "5" 
# => 7
但看在上帝的份上,别那么做

让你们完全困惑的是,这里有一个令人兴奋的场景,你们实际上可以超越单个对象

 a = "5"
 a + "2"
 # => "52"

 def a.+(other)
   self.to_i + other.to_i
 end

 a + "2"
 # => 7

 #but 

 "5" + "2"
 # => "52"
所以在本例中,我们只是覆盖了一个对象,
a
对象,而不是整个字符串类

您将无法以这种方式覆盖整数对象,因为它们是
singleton
…在单个对象基础上无法覆盖的对象

让我们玩一玩

2 + 5 = 7 
2.class = Integer
5.class = Integer 

'2' + '5' = '25'
'2'.class = String
'5'.class = String
所以整数
+
是数字加

字符串
+
是串联等价的

。。。但是为什么呢

您需要了解的是,Ruby中的整数是一个对象,字符串也是一个对象。所有对象(在任何OOP语言中)都有可以调用的方法:

 2.public_methods
 => [:%, :&, :*, :+, :-, :/, :<, :>, :^, :|, :~, :-@, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :[], :inspect, :size, :succ, :to_int, :to_s, :to_i, :to_f, :next, :div, :upto,  ....and the list goes on

'2'.public_methods
 => [:include?, :%, :*, :+, :to_c, :count, :unicode_normalize, :unicode_normalize!, :unicode_normalized?, :partition, :unpack, :unpack1, :sum, :next,  ...and the list goes on
实际上您正在调用一个方法
+

2.+(5)
'2'.+('5')
但实际上,在OOP语言中调用方法实际上是向对象“发送”消息:

2.send(:+, 5)
'2'.send(:+, '5')
反正

您需要了解的一件事是,如果您将不同的对象混合在一起,如:

'2' + 5
# => TypeError: no implicit conversion of Integer into String

2 + '5'
# => TypeError: String can't be coerced into Integer
Ruby将通过引发错误来保护您。以防万一你或你的同事在你的代码里做了些蠢事

但是没有什么可以阻止您将一种对象类型转换为另一种对象类型

'2'.to_i + 5 
 #=> 7

 2 + '5'.to_i
 # => 7

 '2' + 5.to_s
 # => '25'
完全面向对象语言(如Ruby)的另一个有趣部分是,您可以使用
+
方法创建自己的对象

class SafePlus
  def initialize(first_value)
    @first_value = first_value
  end

  def +(other_value) 
    @first_value.to_i + other_value.to_i
  end
end

SafePlus.new(2) + '5'
# => 7

SafePlus.new("5") + "107"
# => 112
您还可以重写String类或Integer类的原始实现,以便所有相应对象的行为如下:

module MyIntegerFix
  def +(other)
    super(other.to_i)
  end
end
Integer.prepend(MyIntegerFix)

2 + "5" 
# => 7
但看在上帝的份上,别那么做

让你们完全困惑的是,这里有一个令人兴奋的场景,你们实际上可以超越单个对象

 a = "5"
 a + "2"
 # => "52"

 def a.+(other)
   self.to_i + other.to_i
 end

 a + "2"
 # => 7

 #but 

 "5" + "2"
 # => "52"
所以在本例中,我们只是覆盖了一个对象,
a
对象,而不是整个字符串类

您将无法以这种方式覆盖整数对象,因为它们是
singleton
…在单个对象基础上无法覆盖的对象


您可以通过执行
var1.to_.s.class
2.class
来检查对象的类别/类型。现在我明白了,感觉像个巨大的假人。谢谢@sepp2k!您可以通过执行
var1.to_.s.class
2.class
来检查对象的类别/类型。现在我明白了,感觉像个巨大的假人。谢谢@sepp2k!没有从一种类型的对象转换到另一种类型的对象,Ruby是一种消息传递语言,如下面的答案所述;您正在将消息
#+
传递给类字符串的对象
var1.to_s
。传递消息时,实例(或类)中定义的任何方法都将被调用:
put var1+var2。另一方面,to_i
…没有从一种类型的对象转换到另一种类型的对象,Ruby是一种消息传递语言,如下所述;您正在将消息
#+
传递给类字符串的对象
var1.to_s
。当您传递消息时,在