Introduction to Ruby 1th/5 page _ruby topics

Source: Internet
Author: User
Tags parse error
First, the method

Ruby's method definition allows you to set a default value for a parameter, but you cannot have a parameter without a default value after an argument with a default value (Allow * and &), meaning that the following method definition is not allowed, and parse error occurs when interpreted. Another point, unlike C #, is that the method definition does not come after the method call.
  
   
   
&args EndShows () # appears after the show call is the wrong def showing end

Ruby also supports parameter features such as C # params, only Ruby uses the * logo.

  
   
   

Similarly, Ruby has an application similar to C # delegate, just simpler, directly expressed in &, and Ruby uses a keyword called yield to tell the interpreter to execute the incoming code block or the Proc object. )。

  
   
   
1def Display (&block

Block_given? is a method defined in the internal module Kernel to indicate whether the Proc object was passed in. Ruby then uses yield to notify the interpreter to execute the incoming Proc. Process objects can also have parameters, unlike normal methods where the parameters of the process object are in a group | | Among You can use the call method of Proc object to invoke a procedure object with parameters.

  
   
   
&block3 @username, @age, @block = Username, age, blocks 4 End 5 6 def Display (TXT) 7 # Although @block is an instance variable, it must be added here Curly braces 8 print "#{@block. Call (TXT)}: # @username-# @age" 9 10end 12emp = employee.new ("Proshea",) {|txt| txt 15 } 16emp. Display ("context")
1
Current 1/5 page 12345 Next read the full text
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.