File: //usr/local/rvm/gems/ruby-2.5.9/doc/activesupport-6.1.4.1/ri/Object/try-i.ri
U:RDoc::GhostMethod[iI"try:ETI"Object#try;TF:publico:RDoc::Markup::Document:@parts[(o:RDoc::Markup::Paragraph; [I"KInvokes the public method whose name goes as first argument just like ;TI"P+public_send+ does, except that if the receiver does not respond to it the ;TI"9call returns +nil+ rather than raising an exception.;To:RDoc::Markup::BlankLine o;
; [I"/This method is defined to be able to write;T@o:RDoc::Markup::Verbatim; [I"@person.try(:name)
;T:@format0o;
; [I"instead of;T@o;; [I"@person.name if @person
;T;
0o;
; [I" +try+ calls can be chained:;T@o;; [I"%@person.try(:spouse).try(:name)
;T;
0o;
; [I"instead of;T@o;; [I"6@person.spouse.name if @person && @person.spouse
;T;
0o;
; [I"Q+try+ will also return +nil+ if the receiver does not respond to the method:;T@o;; [I"0@person.try(:non_existing_method) # => nil
;T;
0o;
; [I"instead of;T@o;; [I"W@person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
;T;
0o;
; [I"P+try+ returns +nil+ when called on +nil+ regardless of whether it responds ;TI"to the method:;T@o;; [I",nil.try(:to_i) # => nil, rather than 0
;T;
0o;
; [I"AArguments and blocks are forwarded to the method if invoked:;T@o;; [I"*@posts.try(:each_slice, 2) do |a, b|
;TI" ...
;TI" end
;T;
0o;
; [I"QThe number of arguments in the signature must match. If the object responds ;TI"Mto the method the call is attempted and +ArgumentError+ is still raised ;TI""in case of argument mismatch.;T@o;
; [I"LIf +try+ is called without arguments it yields the receiver to a given ;TI"block unless it is +nil+:;T@o;; [I"@person.try do |p|
;TI" ...
;TI" end
;T;
0o;
; [I"UYou can also call try with a block without accepting an argument, and the block ;TI"&will be instance_eval'ed instead:;T@o;; [I")@person.try { upcase.truncate(50) }
;T;
0o;
; [I"RPlease also note that +try+ is defined on +Object+. Therefore, it won't work ;TI"Pwith instances of classes that do not have +Object+ among their ancestors, ;TI"-like direct subclasses of +BasicObject+.;T:
@fileI".lib/active_support/core_ext/object/try.rb;T:0@omit_headings_from_table_of_contents_below0I"try(*a, &b)
;T0[ I"