Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Pathname#open
(?::string | ::int mode, ?::int perm, ?flags: ::Integer, ?external_encoding: ::encoding, ?internal_encoding: ::encoding, ?encoding: ::encoding, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?path: ::path, ?invalid: :replace | nil, ?undef: :replace | nil, ?replace: ::String | nil, ?fallback: ::Hash[::string, ::string] | ^(::String) -> ::string | ::Method | nil, ?xml: :text | :attr | nil, ?cr_newline: bool, ?crlf_newline: bool, ?universal_newline: bool) -> ::File
[T] (?::string | ::int mode, ?::int perm, ?mode: ::Integer | ::String, ?flags: ::Integer, ?external_encoding: ::encoding, ?internal_encoding: ::encoding, ?encoding: ::encoding, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?path: ::path, ?invalid: :replace | nil, ?undef: :replace | nil, ?replace: ::String | nil, ?fallback: ::Hash[::string, ::string] | ^(::String) -> ::string | ::Method | nil, ?xml: :text | :attr | nil, ?cr_newline: bool, ?crlf_newline: bool, ?universal_newline: bool) { (::File) -> T } -> T
open(mode = 'r', permissions = 0666, **options) {|file| ... } → object
open(mode = 'r', permissions = 0666, **options) → file
pathname_builtin.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 1759
def open(...) # :yield: file
File.open(@path, ...)
end
Creates a File object for the entry at the path in self and opens the file; see File.new for details.
With a block given, calls the block with the file; closes the file on block exit:
Example
Pathname('README.md').open {|file| file.size } # => 3469
With no block given, returns the file:
Example
file = Pathname('README.md').open # => #<File:README.md> file.size # => 3469 file.close