Assignment related operations in Ruby programming and ruby programming assignment

Source: Internet
Author: User

Assignment related operations in Ruby programming and ruby programming assignment

In earlier versions of Ruby, the return value of the value assignment statement is the return value of the method that sets this attribute. In Ruby1.8, the value of the value assignment statement is always the value of the parameter, and the return value of the method is lost.
 

class Test
  def val=(val)
    @val = val
    return 99
  end    
end
 
t = Test.new
a = t.val=2
a           ->2

In the old version, a is set to 99 by the value assignment statement, and in Ruby1.8, its value is 2.

Ruby's assignment is actually executed in parallel, so the value on the right of the assignment statement is not affected by the assignment statement itself. Before any variable or attribute on the left is assigned a value, the values on the right are calculated in the order they appear. The following is an example of human design. In the second row, values of the expressions x, x + = 1, and x + = 1 are assigned to variables a, B, and c respectively.
 

x = 0                 -> 0
a,b,c =x,(x+=1),(x+=1)   ->[0,1,2]

When the value assignment statement has more than one left value, the value assignment expression returns an array consisting of the right value. If the left value of the value assignment statement is greater than the right value, the extra left value is ignored. If the right value is left, the extra right value is ignored. If the value assignment statement has only one left value and multiple right values, the right value is converted to an array and then assigned to the left value.

In fact, writable attributes have a hidden trap. Generally, methods in a class can call other methods of the same class and their parent class methods in the form of functions (that is, using an implicit self as the receiver. However, this does not apply to attribute assignment functions: when Ruby sees the assignment statement, it considers the name on the left as a local variable rather than a method call to assign values to the attribute.
 

class BrokenAmplifier
 attr_accessor :left_channel, :right_channel
 def volume=(vol)
   left_channel = self.right_channel = vol     
 end  
end
 
ba = BrokenAmplifier.new
ba.left_channel = ba.right_channel =99
ba.volume=5
ba.left_channel          ->99
ba.right_channel         ->5

In the above assignment statement, we forgot to add the "self." prefix to left_channel. Therefore, Ruby stores the new value to the local variable of the volume = method, because the object property is not updated at all. This will become a difficult-to-track Defect


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.