Ruby object variables and why are they always equal to the same name?

In the books that I read, I always see

def initialize (side_length)
    @side_length = side_length
end

If an object variable is always equal to a variable with the same name, why not write down the language so that it simply equals it without entering it?

For instance:

def initialize (side_length)
  @side_length # this could just equal side_length without stating it
end

Thus, we do not need to enter it on top.

+4
source share
2 answers

In the above example:

def initialize(side_length)
  @side_length = side_length
end

@side_length = side_length is just an assignment that passes the available argument to the instance variable, in this case it is the same name as the argument.

However, these two values should not have the same name . This is usually called for readability / consistency. The same code could have been just as easy:

def initialize(side_length)
  @muffin = side_length
end

, ( -, ). , .

, , , , , , , side_length @muffin .

SO > , - .

, !

+1

, " [ ] [] [sic] ".

0

All Articles