One-step learning of Ruby series (2): Functions in ruby

Source: Internet
Author: User

I. No parameter functions:

 

IRB (main ): 010 : 0 >   Def H
IRB (main ): 011 : 1 > Puts " Hello world! "
IRB (main ): 012 : 1 > End
=> Nil

IRB (main ):013: 0>H
Hello world!
=>Nil
IRB (main ):014: 0>H ()
Hello world!
=>Nil

Calling a function in ruby is as simple as calling a ruby name.If a function does not need to accept a parameter, you only need to mention it. You can add a pair of parentheses, but they are not required.

Ii. Functions with Parameters

IRB (main ): 015 : 0 >   Def H (name)
IRB (main ): 016 : 1 > Puts " Hello # {name }! "
IRB (main ): 017 : 1 > End
=> Nil
IRB (main): 0 18 : 0 > H ( " Matz " )
Hello Matz!
=> Nil

You can use # {parametername} to reference parameters. Wait, it seems that the parameter has no parameter type. Yes, this is the embodiment of the famous "Duck.

3. Default Parameters

IRB (main): 0 19 : 0 >   Def H (name =   " World " )
IRB (main ): 020 : 1 > Puts " Hello # {name. capitalize }! "
IRB (main ): 021 : 1 > End
=> Nil
IRB (main ): 022 : 0 >   H "Chris"
Hello Chris!
=> Nil
IRB (main ): 023 : 0 > H
Hello world!
=> Nil

 

Parameters can be specified in parentheses or directly.

If a parameter has a default value, you can choose not to specify the parameter and call it as if there is no Parameter Function.

Problem:

The Return Value of the parameter.

Multi-parameter function call problems.

In a multi-parameter function, must the default parameter position be at the end?

Okay. Next I will talk about classes in ruby.

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.