⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.25
Server IP:
109.199.105.153
Server:
Linux connect.inboxifs.com 5.15.0-152-generic #162-Ubuntu SMP Wed Jul 23 09:48:42 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
8.2.29
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
ri
/
3.0.0
/
system
/
syntax
/
View File Name :
page-calling_methods_rdoc.ri
U:RDoc::TopLevel[ i I" syntax/calling_methods.rdoc:EFcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[�S:RDoc::Markup::Heading: leveli: textI"Calling Methods;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"OCalling a method sends a message to an object so it can perform some work.;T@ o; ;[I"7In ruby you send a message to an object like this:;T@ o:RDoc::Markup::Verbatim;[I"my_method() ;T:@format0o; ;[I",Note that the parenthesis are optional:;T@ o;;[I"my_method ;T;0o; ;[I"RExcept when there is difference between using and omitting parentheses, this ;TI"Mdocument uses parenthesis when arguments are present to avoid confusion.;T@ o; ;[I"SThis section only covers calling methods. See also the {syntax documentation ;TI"8on defining methods}[rdoc-ref:syntax/methods.rdoc].;T@ S; ; i;I" Receiver;T@ o; ;[I"T+self+ is the default receiver. If you don't specify any receiver +self+ will ;TI"8be used. To specify a receiver use <code>.</code>:;T@ o;;[I"my_object.my_method ;T;0o; ;[I"MThis sends the +my_method+ message to +my_object+. Any object can be a ;TI"Ureceiver but depending on the method's visibility sending a message may raise a ;TI"NoMethodError.;T@ o; ;[I"RYou may also use <code>::</code> to designate a receiver, but this is rarely ;TI"Qused due to the potential for confusion with <code>::</code> for namespaces.;T@ S; ; i;I"Chaining \Method Calls;T@ o; ;[I"XYou can "chain" method calls by immediately following one method call with another.;T@ o; ;[I"@This example chains methods Array#append and Array#compact:;T@ o;;[ I"a = [:foo, 'bar', 2] ;TI"!a1 = [:baz, nil, :bam, nil] ;TI" a2 = a.append(*a1).compact ;TI"*a2 # => [:foo, "bar", 2, :baz, :bam] ;T;0o; ;[I" Details:;T@ o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o; ;[I"?First method <tt>merge</tt> creates a copy of <tt>a</tt>, ;TI"Nappends (separately) each element of <tt>a1</tt> to the copy, and returns;To;;[I",[:foo, "bar", 2, :baz, nil, :bam, nil] ;T;0o;;0;[o; ;[I"JChained method <tt>compact</tt> creates a copy of that return value, ;TI"9removes its <tt>nil</tt>-valued entries, and returns;To;;[I""[:foo, "bar", 2, :baz, :bam] ;T;0o; ;[I":You can chain methods that are in different classes. ;TI"=This example chains methods Hash#to_a and Array#reverse:;T@ o;;[I""h = {foo: 0, bar: 1, baz: 2} ;TI";h.to_a.reverse # => [[:baz, 2], [:bar, 1], [:foo, 0]] ;T;0o; ;[I" Details:;T@ o;;;;[o;;0;[o; ;[I"IFirst method Hash#to_a converts <tt>a</tt> to an \Array, and returns;To;;[I"'[[:foo, 0], [:bar, 1], [:baz, 2]] ;T;0o;;0;[o; ;[I"EChained method Array#reverse creates copy of that return value, ;TI"reverses it, and returns;To;;[I"'[[:baz, 2], [:bar, 1], [:foo, 0]] ;T;0S; ; i;I"Safe Navigation Operator;T@ o; ;[I"T<code>&.</code>, called "safe navigation operator", allows to skip method call ;TI"Vwhen receiver is +nil+. It returns +nil+ and doesn't evaluate method's arguments ;TI"if the call is skipped.;T@ o;;[I" REGEX = /(ruby) is (\w+)/i ;TI"5"Ruby is awesome!".match(REGEX).values_at(1, 2) ;TI"# => ["Ruby", "awesome"] ;TI";"Python is fascinating!".match(REGEX).values_at(1, 2) ;TI"D# NoMethodError: undefined method `values_at' for nil:NilClass ;TI"<"Python is fascinating!".match(REGEX)&.values_at(1, 2) ;TI"# => nil ;T;0o; ;[I"SThis allows to easily chain methods which could return empty value. Note that ;TI"U<code>&.</code> skips only one next call, so for a longer chain it is necessary ;TI"#to add operator on each level:;T@ o;;[ I"H"Python is fascinating!".match(REGEX)&.values_at(1, 2).join(' - ') ;TI"?# NoMethodError: undefined method `join' for nil:NilClass ;TI"I"Python is fascinating!".match(REGEX)&.values_at(1, 2)&.join(' - ') ;TI"# => nil ;T;0S; ; i;I"Arguments;T@ o; ;[ I"OThere are three types of arguments when sending a message, the positional ;TI"Sarguments, keyword (or named) arguments and the block argument. Each message ;TI"Psent may use one, two or all types of arguments, but the arguments must be ;TI"supplied in this order.;T@ o; ;[I"PAll arguments in ruby are passed by reference and are not lazily evaluated.;T@ o; ;[I"4Each argument is separated by a <code>,</code>:;T@ o;;[I"my_method(1, '2', :three) ;T;0o; ;[I"5Arguments may be an expression, a hash argument:;T@ o;;[I"'key' => value ;T;0o; ;[I"or a keyword argument:;T@ o;;[I"key: value ;T;0o; ;[I"MHash and keyword arguments must be contiguous and must appear after all ;TI",positional arguments, but may be mixed:;T@ o;;[I")my_method('a' => 1, b: 2, 'c' => 3) ;T;0S; ; i;I"Positional Arguments;T@ o; ;[I"EThe positional arguments for the message follow the method name:;T@ o;;[I"%my_method(argument1, argument2) ;T;0o; ;[I"IIn many cases, parenthesis are not necessary when sending a message:;T@ o;;[I"$my_method argument1, argument2 ;T;0o; ;[I"OHowever, parenthesis are necessary to avoid ambiguity. This will raise a ;TI"RSyntaxError because ruby does not know which method argument3 should be sent ;TI"to:;T@ o;;[I";method_one argument1, method_two argument2, argument3 ;T;0o; ;[I"LIf the method definition has a <code>*argument</code> extra positional ;TI"Harguments will be assigned to +argument+ in the method as an Array.;T@ o; ;[I"PIf the method definition doesn't include keyword arguments, the keyword or ;TI"Lhash-type arguments are assigned as a single hash to the last argument:;T@ o;;[ I"def my_method(options) ;TI" p options ;TI" end ;TI" ;TI"9my_method('a' => 1, b: 2) # prints: {'a'=>1, :b=>2} ;T;0o; ;[I"LIf too many positional arguments are given, an ArgumentError is raised.;T@ S; ; i;I"!Default Positional Arguments;T@ o; ;[I"QWhen the method defines default arguments you do not need to supply all the ;TI"Parguments to the method. Ruby will fill in the missing arguments in-order.;T@ o; ;[I"QFirst we'll cover the simple case where the default arguments appear on the ;TI""right. Consider this method:;T@ o;;[I"'def my_method(a, b, c = 3, d = 4) ;TI" p [a, b, c, d] ;TI" end ;T;0o; ;[I"QHere +c+ and +d+ have default values which ruby will apply for you. If you ;TI",send only two arguments to this method:;T@ o;;[I"my_method(1, 2) ;T;0o; ;[I"7You will see ruby print <code>[1, 2, 3, 4]</code>.;T@ o; ;[I"!If you send three arguments:;T@ o;;[I"my_method(1, 2, 5) ;T;0o; ;[I"6You will see ruby print <code>[1, 2, 5, 4]</code>;T@ o; ;[I"<Ruby fills in the missing arguments from left to right.;T@ o; ;[I"QRuby allows default values to appear in the middle of positional arguments. ;TI"+Consider this more complicated method:;T@ o;;[I"'def my_method(a, b = 2, c = 3, d) ;TI" p [a, b, c, d] ;TI" end ;T;0o; ;[I"SHere +b+ and +c+ have default values. If you send only two arguments to this ;TI"method:;T@ o;;[I"my_method(1, 4) ;T;0o; ;[I"7You will see ruby print <code>[1, 2, 3, 4]</code>.;T@ o; ;[I"!If you send three arguments:;T@ o;;[I"my_method(1, 5, 6) ;T;0o; ;[I"7You will see ruby print <code>[1, 5, 3, 6]</code>.;T@ o; ;[I"ODescribing this in words gets complicated and confusing. I'll describe it ;TI"%in variables and values instead.;T@ o; ;[ I"QFirst <code>1</code> is assigned to +a+, then <code>6</code> is assigned to ;TI"F+d+. This leaves only the arguments with default values. Since ;TI"Q<code>5</code> has not been assigned to a value yet, it is given to +b+ and ;TI"2+c+ uses its default value of <code>3</code>.;T@ S; ; i;I"Keyword Arguments;T@ o; ;[I"SKeyword arguments follow any positional arguments and are separated by commas ;TI"like positional arguments:;T@ o;;[I"@my_method(positional1, keyword1: value1, keyword2: value2) ;T;0o; ;[ I"PAny keyword arguments not given will use the default value from the method ;TI"Odefinition. If a keyword argument is given that the method did not list, ;TI"Oand the method definition does not accept arbitrary keyword arguments, an ;TI""ArgumentError will be raised.;T@ S; ; i;I"Block Argument;T@ o; ;[I"MThe block argument sends a closure from the calling scope to the method.;T@ o; ;[I"TThe block argument is always last when sending a message to a method. A block ;TI"Ois sent to a method using <code>do ... end</code> or <code>{ ... }</code>:;T@ o;;[I"my_method do ;TI" # ... ;TI" end ;T;0o; ;[I"or:;T@ o;;[I"my_method { ;TI" # ... ;TI"} ;T;0o; ;[I"G<code>do end</code> has lower precedence than <code>{ }</code> so:;T@ o;;[I"method_1 method_2 { ;TI" # ... ;TI"} ;T;0o; ;[I")Sends the block to +method_2+ while:;T@ o;;[I"method_1 method_2 do ;TI" # ... ;TI" end ;T;0o; ;[I"TSends the block to +method_1+. Note that in the first case if parentheses are ;TI"*used the block is sent to +method_1+.;T@ o; ;[ I"RA block will accept arguments from the method it was sent to. Arguments are ;TI"Sdefined similar to the way a method defines arguments. The block's arguments ;TI"Igo in <code>| ... |</code> following the opening <code>do</code> or ;TI"<code>{</code>:;T@ o;;[I")my_method do |argument1, argument2| ;TI" # ... ;TI" end ;T;0S; ; i ;I"Block Local Arguments;T@ o; ;[I"SYou may also declare block-local arguments to a block using <code>;</code> in ;TI"Mthe block arguments list. Assigning to a block-local argument will not ;TI"Foverride local arguments outside the block in the caller's scope:;T@ o;;[I"def my_method ;TI" yield self ;TI" end ;TI" ;TI"place = "world" ;TI" ;TI"my_method do |obj; place| ;TI" place = "block" ;TI", puts "hello #{obj} this is #{place}" ;TI" end ;TI" ;TI"puts "place is: #{place}" ;T;0o; ;[I"This prints:;T@ o;;[I"hello main this is block ;TI"place is world ;T;0o; ;[I"NSo the +place+ variable in the block is not the same +place+ variable as ;TI"Poutside the block. Removing <code>; place</code> from the block arguments ;TI"gives this result:;T@ o;;[I"hello main this is block ;TI"place is block ;T;0S; ; i;I""Array to Arguments Conversion;T@ o; ;[I" Given the following method:;T@ o;;[I"4def my_method(argument1, argument2, argument3) ;TI" end ;T;0o; ;[I"PYou can turn an Array into an argument list with <code>*</code> (or splat) ;TI"operator:;T@ o;;[I"arguments = [1, 2, 3] ;TI"my_method(*arguments) ;T;0o; ;[I"or:;T@ o;;[I"arguments = [2, 3] ;TI"my_method(1, *arguments) ;T;0o; ;[I"Both are equivalent to:;T@ o;;[I"my_method(1, 2, 3) ;T;0o; ;[I"PIf the method accepts keyword arguments, the splat operator will convert a ;TI"9hash at the end of the array into keyword arguments:;T@ o;;[ I"def my_method(a, b, c: 3) ;TI" end ;TI" ;TI""arguments = [1, 2, { c: 4 }] ;TI"my_method(*arguments) ;T;0o; ;[I"NNote that this behavior is currently deprecated and will emit a warning. ;TI"/This behavior will be removed in Ruby 3.0.;T@ o; ;[I"RYou may also use the <code>**</code> (described next) to convert a Hash into ;TI"keyword arguments.;T@ o; ;[I"TIf the number of objects in the Array do not match the number of arguments for ;TI"1the method, an ArgumentError will be raised.;T@ o; ;[I"PIf the splat operator comes first in the call, parentheses must be used to ;TI"avoid a warning:;T@ o;;[I"%my_method *arguments # warning ;TI"(my_method(*arguments) # no warning ;T;0S; ; i;I")Hash to Keyword Arguments Conversion;T@ o; ;[I" Given the following method:;T@ o;;[I"2def my_method(first: 1, second: 2, third: 3) ;TI" end ;T;0o; ;[I"IYou can turn a Hash into keyword arguments with the <code>**</code> ;TI"(keyword splat) operator:;T@ o;;[I"3arguments = { first: 3, second: 4, third: 5 } ;TI"my_method(**arguments) ;T;0o; ;[I"or:;T@ o;;[I")arguments = { first: 3, second: 4 } ;TI"&my_method(third: 5, **arguments) ;T;0o; ;[I"Both are equivalent to:;T@ o;;[I".my_method(first: 3, second: 4, third: 5) ;T;0o; ;[I"AIf the method definition uses the keyword splat operator to ;TI"Cgather arbitrary keyword arguments, they will not be gathered ;TI"by <code>*</code>:;T@ o;;[ I"def my_method(*a, **kw) ;TI"$ p arguments: a, keywords: kw ;TI" end ;TI" ;TI"(my_method(1, 2, '3' => 4, five: 6) ;T;0o; ;[I"Prints:;T@ o;;[I"9{:arguments=>[1, 2], :keywords=>{'3'=>4, :five=>6}} ;T;0S; ; i;I"Proc to Block Conversion;T@ o; ;[I"%Given a method that use a block:;T@ o;;[I"def my_method ;TI" yield self ;TI" end ;T;0o; ;[I"RYou can convert a proc or lambda to a block argument with the <code>&</code> ;TI"!(block conversion) operator:;T@ o;;[I"=argument = proc { |a| puts "#{a.inspect} was yielded" } ;TI" ;TI"my_method(&argument) ;T;0o; ;[I"SIf the block conversion operator comes first in the call, parenthesis must be ;TI"used to avoid a warning:;T@ o;;[I"$my_method &argument # warning ;TI"'my_method(&argument) # no warning ;T;0S; ; i;I"Method Lookup;T@ o; ;[I"TWhen you send a message, Ruby looks up the method that matches the name of the ;TI"Tmessage for the receiver. Methods are stored in classes and modules so method ;TI"4lookup walks these, not the objects themselves.;T@ o; ;[I"OHere is the order of method lookup for the receiver's class or module +R+:;T@ o;;;;[o;;0;[o; ;[I"2The prepended modules of +R+ in reverse order;To;;0;[o; ;[I"!For a matching method in +R+;To;;0;[o; ;[I"1The included modules of +R+ in reverse order;T@ o; ;[I"QIf +R+ is a class with a superclass, this is repeated with +R+'s superclass ;TI"until a method is found.;T@ o; ;[I"/Once a match is found method lookup stops.;T@ o; ;[I"KIf no match is found this repeats from the beginning, but looking for ;TI"S+method_missing+. The default +method_missing+ is BasicObject#method_missing ;TI"+which raises a NameError when invoked.;T@ o; ;[I"UIf refinements (an experimental feature) are active, the method lookup changes. ;TI"OSee the {refinements documentation}[rdoc-ref:syntax/refinements.rdoc] for ;TI" details.;T: @file@:0@omit_headings_from_table_of_contents_below0