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. )。
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