Initialization methods for Ruby objects _ruby topics

Source: Internet
Author: User
The Fruit class in the previous section has two real variables, describe the type and state of the fruit. Until a custom inspect method is written for this class, we understand that it does not make a reasonable explanation for a fruit that lacks attributes. Fortunately, Ruby provides a way to allow real variables to always be initialized.


Initalize method

When Ruby creates a new object, it always looks for a method named Initialize and executes it. Therefore, we can simply add the default value to the real variable by a initialize method, so that the inspect method has something to say.

Ruby> class Fruit
| DEF initialize
| @kind = "Apple"
| @condition = "ripe"
| End
| End
Nil
ruby> F4 = fruit.new
"A ripe apple"


The default is changed to the required

Most of the time, the defaults don't mean too much. Is it really the fruit of the default? It may be a better way to make a request for the type of fruit when it is created. To do so, we must add a formal parameter to the Initialize method. Because some of the reasons that won't be mentioned here, you go to new The parameters passed are actually handed to the initialize.

Ruby> class Fruit
| Def initialize (k)
| @kind = k
| @condition = "ripe"
| End
| End
Nil
ruby> f5 = fruit.new "Mango"
"A ripe mango"
ruby> F6 = fruit.new
ERR: (eval): 1:in ' Initialize ': Wrong # of arguments (0 for 1)


The initialization of elasticity

Above we see that once a parameter is associated with a initialize method, it cannot be omitted without error generation. If you want to think about it, we can use it if we give the parameter, otherwise we will use the default value.

Ruby> class Fruit
| def initialize (k= "Apple")
| @kind = k
| @condition = "ripe"
| End
| End
Nil
ruby> f5 = fruit.new "Mango"
"A ripe mango"
ruby> F6 = fruit.new
"A ripe apple"


You can use default parameters in any method, not just initialize. The parameter table (argument list) must end with a parameter with a default value.

Sometimes, it is useful to provide a variety of initialization object methods. Although it is beyond the scope of this tutorial, Ruby provides object mappings (objects reflection) and variable-length parameter tables (variable-length argument lists). These all contribute effectively to the method overload.
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.