ÿØÿà JFIF ÿÛ „ ( %!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
ÿØÿà JFIF ÿÛ „
(
%!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ
ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ
ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ
? ¼QxJQaÍuò¸Zö
Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
????????????????????????????????????
????????????????????????????????????
ÿØÿà JFIF ÿÛ „ ( %!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
ÿØÿà JFIF ÿÛ „
(
%!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ
ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ
ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ
? ¼QxJQaÍuò¸Zö
Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
????????????????????????????????????
????????????????????????????????????
U:RDoc::TopLevel[ i I" syntax/calling_methods.rdoc:ETcRDoc::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 .:;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 :: to designate a receiver, but this is rarely ;TI"Qused due to the potential for confusion with :: 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 merge creates a copy of a, ;TI"Nappends (separately) each element of a1 to the copy, and returns;To;;[I",[:foo, "bar", 2, :baz, nil, :bam, nil]
;T;0o;;0;[o;
;[I"JChained method compact creates a copy of that return value, ;TI"9removes its nil-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 a 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&., 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&. 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 ,:;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 *argument 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 [1, 2, 3, 4].;T@
o;
;[I"!If you send three arguments:;T@
o;;[I"my_method(1, 2, 5)
;T;0o;
;[I"6You will see ruby print [1, 2, 5, 4];T@
o;
;[I"[1, 2, 3, 4].;T@
o;
;[I"!If you send three arguments:;T@
o;;[I"my_method(1, 5, 6)
;T;0o;
;[I"7You will see ruby print [1, 5, 3, 6].;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 1 is assigned to +a+, then 6 is assigned to ;TI"F+d+. This leaves only the arguments with default values. Since ;TI"Q5 has not been assigned to a value yet, it is given to +b+ and ;TI"2+c+ uses its default value of 3.;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@
o;
;[I"NKeyword argument value can be omitted, meaning the value will be fetched ;TI",from the context by the name of the key;T@
o;;[ I"keyword1 = 'some value'
;TI"'my_method(positional1, keyword1:)
;TI"# ...is the same as
;TI"0my_method(positional1, keyword1: keyword1)
;T;0o;
;[I"UBe aware that when method parenthesis are omitted, too, the parsing order might ;TI"be unexpected:;T@
o;;[I"&my_method positional1, keyword1:
;TI"
;TI"some_other_expression
;TI"
;TI" # ...is actually parsed as
;TI"=my_method(positional1, keyword1: some_other_expression)
;T;0S; ;
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 do ... end or { ... }:;T@
o;;[I"my_method do
;TI"
# ...
;TI" end
;T;0o;
;[I"or:;T@
o;;[I"my_method {
;TI"
# ...
;TI"}
;T;0o;
;[I"Gdo end has lower precedence than { } 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 | ... | following the opening do or ;TI"{:;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 ; 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 ; place from the block arguments ;TI"gives this result:;T@
o;;[I"hello main this is block
;TI"place is block
;T;0S; ;
i;I"#Unpacking Positional Arguments;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 * (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"RThe * unpacking operator can be applied to any object, not only ;TI"Parrays. If the object responds to a #to_a method, this method ;TI"Zis called, and is expected to return an Array, and elements of this array are passed ;TI"&as separate positional arguments:;T@
o;;[I"class Name
;TI" def initialize(name)
;TI" @name = name
;TI" end
;TI"
;TI"# def to_a = @name.split(' ')
;TI" end
;TI"
;TI"!name = Name.new('Jane Doe')
;TI"p(*name)
;TI"# prints separate values:
;TI"# Jane
;TI"
# Doe
;T;0o;
;[I"YIf the object doesn't have a #to_a method, the object itself is passed ;TI"as one argument:;T@
o;;[I"class Name
;TI" def initialize(name)
;TI" @name = name
;TI" end
;TI" end
;TI"
;TI"!name = Name.new('Jane Doe')
;TI"p(*name)
;TI"!# Prints the object itself:
;TI"5# #
;T;0o;
;[I"WThis allows to handle one or many arguments polymorphically. Note also that +nil+ ;TI"Uhas NilClass#to_a defined to return an empty array, so conditional unpacking is ;TI"possible:;T@
o;;[I"5my_method(*(some_arguments if some_condition?))
;T;0o;
;[I"VIf #to_a method exists and does not return an Array, it would be an ;TI"error on unpacking:;T@
o;;[I"class Name
;TI" def initialize(name)
;TI" @name = name
;TI" end
;TI"
;TI" def to_a = @name
;TI" end
;TI"
;TI"!name = Name.new('Jane Doe')
;TI"p(*name)
;TI"I# can't convert Name to Array (Name#to_a gives String) (TypeError)
;T;0o;
;[I"RYou may also use the ** (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"Uavoid an ambiguity of interpretation as an unpacking operator or multiplication ;TI"Coperator. In this case, Ruby issues a warning in verbose mode:;T@
o;;[I"Imy_method *arguments # warning: '*' interpreted as argument prefix
;TI"(my_method(*arguments) # no warning
;T;0S; ;
i;I" Unpacking Keyword Arguments;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 ** ;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"SThe ** unpacking operator can be applied to any object, not only ;TI"Shashes. If the object responds to a #to_hash method, this method ;TI"Xis called, and is expected to return an Hash, and elements of this hash are passed ;TI"as keyword arguments:;T@
o;;[I"class Name
;TI" def initialize(name)
;TI" @name = name
;TI" end
;TI"
;TI"R def to_hash = {first: @name.split(' ').first, last: @name.split(' ').last}
;TI" end
;TI"
;TI"!name = Name.new('Jane Doe')
;TI"p(**name)
;TI"+# Prints: {name: "Jane", last: "Doe"}
;T;0o;
;[ I"UUnlike * operator, ** raises an error when used on an ;TI"Pobject that doesn't respond to #to_hash. The one exception is ;TI"Q+nil+, which doesn't explicitly define this method, but is still allowed to ;TI"Lbe used in ** unpacking, not adding any keyword arguments.;T@
o;
;[I"2Again, this allows for conditional unpacking:;T@
o;;[I"Jmy_method(some: params, **(some_extra_params if pass_extra_params?))
;T;0o;
;[I"\Like * operator, ** raises an error when the object responds ;TI"#to_hash, but it doesn't return a Hash.;T@
o;
;[I"AIf the method definition uses the keyword splat operator to ;TI"Cgather arbitrary keyword arguments, they will not be gathered ;TI"by *:;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 & ;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