Getting Started with Ruby language

Source: Internet
Author: User
Tags parse error



I. Method

Ruby's method definition allows setting default values for parameters, but parameters without default values cannot appear after parameters with default values (allowing * and &), which means that the following method definitions are not allowed, explain A parse error occurs. Another difference from C # is that method definitions cannot appear after method calls.

# parse error
def Display (args1 = "proshea", args2)
end
# Allow
def Display (args1 = "proshea", * args2)
end
# Allow
def Display (args1 = "proshea", & args)
end
Show ()
# Appears wrong after Show call
def Show
end

Ruby also supports parameter functions like C # params, except that Ruby uses * to mark it.

def Display (* args)
print% Q ~ # {args.join ("-")} ~
end
# proshea-32-WinForm
Display ("proshea", 32, "WinForm")

Similarly, Ruby also has an application similar to the C # delegate, but it is simpler and is directly expressed by &. Ruby uses a keyword called yield to tell the interpreter to execute the passed code block or Proc object ?).

1def Display (& block)
2 if block_given?
3 yield (block)
4 else
5 print% Q ~ no process object passed in ~
6 end
7end
8
9def Show ()
10 print% Q ~ Show method call ~
11end
12
13 # No incoming process object
14Display ()
15 # Call Show method inside Display
16 # Note that the opening brace can still only be on the same line as the method name
17Display () {
18 Show ()
19}



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.