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