Interesting ruby-Study notes 3

Source: Internet
Author: User

Ruby method

The method name starts with a lowercase letter and is used as a constant if you start with a capital letter ( this is odd )

Define a method with no parameters

def method_name    Expr. End

Define a method that has a parameter

def method_name (var1, var2)   expr. End
Set default values for parameters

def method_name (var1=value1, var2=value2)   expr. End

The invocation method is simple

Method_name 25, 30
return value It's a peculiar point .It's ruby. You can default to the value of the last statement as a return value such as
def test   i =   J = Ten   k = 0end
When this method is called, the last declared variable K is returned.

You can also return a value with a regular return statement

It is interesting to note that if you give more than two expressions, the array containing the values will be the return value. If the expression is not given, nil will be the return value

#!/usr/bin/ruby#-*-coding:utf-8-*-def Test   i =   j = k =   300return I, j, Kendvar = testputs var
Return

100200300
Variable number of parameters
#!/usr/bin/ruby#-*-coding:utf-8-*-def sample (*test)   puts "number of arguments #{test.length}" for   I in 0...test.length
   
    puts "parameter value is #{test[i]}"   endendsample "Zara", "6", "F" Sample "Mac", "Min", "M", "MCA"
   
Class method There's a strange default rule: When a method is defined outside a class, the method is marked as private by default. On the other hand, if the method is defined in a class, the default flag is public. Ruby alias statement
Alias method Name Method name alias global variable global variable
Although I really don't know what this is all about.。。。 The Ruby undef statement can also dynamically cancel the method definition ( not only does this feel like it's useless, it's best not to use



Interesting ruby-Study notes 3

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.