I want to look up the description of the Ruby-File.read() method in the API document.

Asked 2 years ago, Updated 2 years ago, 47 views

I'm learning how Ruby works from the beginning.

http://docs.ruby-lang.org/ja/2.1.0/class/File.html

What should I do to find out the read method?

There are singular methods, but where are the non-specific methods?

ruby

2022-09-30 18:28

5 Answers

read is not from the File class. read is a method inherited from the IO class, so it is a method classLook for the IO Class page instead of the code> Class.

Addition: Sorry if you know.

If you look at the IO class, you will notice that there are two read items.They have the same name but are different methods.

There are two types of Ruby: read.read for singular methods and read for instance methods.To distinguish between the two, there is a custom in which the former is written as IO.read, and the latter as IO#read.

For IO.read, pass the file name and read the entire contents on the spot.
text=IO.read("hogehoge.txt").
The complicated thing is that File inherits IO, so text=File.read("hogehoge.txt") works fine.(Maybe this is what Miruka wanted to find out.)

IO#read reads the contents of an already opened file.
open('hogehoge.txt'){|f|text=f.read}.
(Detailed options are not included)

Note: Why IO.read() instead of File.read()?


2022-09-30 18:28

You need to understand the concept of inheritance.
I have two questions, but I heard you're studying, so it's easy...

Here's how to find it.


  • class inheritance list:File<IO<Enumerable<File::Constants<Object<Kernel<BasicObject

Non-specific methods are called instance methods.
On the page you mentioned, atime and chmod are affected.

Good luck with your studies (^^)


2022-09-30 18:28

You can use ri from the shell.

ri File.read

Output:

 = File.read

(from ruby core)
=== Implementation from IO
------------------------------------------------------------------------------
  IO.read(name, [length[, offset]]]) - > string
  IO.read(name, [length[, offset]], open_args) - > string

------------------------------------------------------------------------------

opens the file, optionally sees to the give offset, then returns length
bytes (defaulting to the rest of the file).read senses the file is closed
before returning.

[...snip...]

(from ruby core)
=== Implementation from IO
------------------------------------------------------------------------------
  ios.read([length[, outbuf]]) - > string, outbuf, oril

------------------------------------------------------------------------------

Read length bytes from the I/O stream.

[...snip...]

"Implementation from IO" indicates that it is inherited from IO.(The former describes the class method and the latter describes the instance method.)


2022-09-30 18:28

File.read is a method in the IO class, so check the API documentation for the IO class.

http://docs.ruby-lang.org/ja/2.1.0/class/IO.html#S_READ

... is the answer, but I think beginners will be like, "How do you know the method of the IO class?"

Here are some ways to find out where Ruby's methods are defined:

Using irb+method

From irb, you can expect it to be a IO class of methods:

>File.method(:read) 
=>#<Method: File(IO).read>

By the way, if it's a gem method, you can also find out where the code is (but nil for C language implementations, such as File.read).

>User.method(:find).source_location 
=>["/Users/jit/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.0.8/lib/active_record/querying.rb", 1]

Note: How to find a method definition location in Ruby

Using pry+pry-doc

after installing pry and pry-doc? You can view documents and source codes in File.read and $File.read.

$gem install pri-doc
$ please               
[1] pry(main)>?File.read

From:io.c(CMethod):
Owner:#<Class:IO>
Visibility:public
Signature: read(*arg1)
Number of lines—29

opens the file, optionally sees to the give offset, then returns
length bytes (defaulting to the rest of the file).read
senses the file is closed before returning.

If the last argument is a hash, it specifications option for internal
open().The key would be the following.open_args: is exclusive
others.

encoding::
  string or encoding

  specifications encoding of the read string.[32 encoding [0m will be ignored]
  if length is specified.

mode:
  string

  specifics mode argument for open().It should start with "r"
  otherwise it will cause an error.

open_args::array of strings

  specifications arguments for open() as an array.

Examples:

  IO.read("testfile")#=> "This is line one\nThis is line two\nThis is line three\nAnd son...\n"
  IO.read("testfile", 20)#=> "This is line one\nThi"
  IO.read("testfile", 20, 10)#=>"neone\nThis is line"
[2] pry(main)>$File.read

From:io.c(CMethod):
Owner:#<Class:IO>
Visibility:public
Number of lines—24

static VALUE
rb_io_s_read (intargc, VALUE*argv, VALUEio)
{
    VALUE opt, offset;
    structure foreach_argarg;

    argc=rb_scan_args(argc, argv, "13:", NULL, NULL, & offset, NULL, & opt);
    open_key_args(argc, argv, opt, & arg);
    if(NIL_P(arg.io)) return Qnil;
    if(!NIL_P(offset)){
    structure seek_arg sarg;
    int state = 0;
    sarg.io = arg.io;
    sarg.offset=offset;
    sarg.mode=SEEK_SET;
    rb_protect(seek_before_access, (VALUE)&sarg,&state);
    if(state){
        rb_io_close(arg.io);
        rb_jump_tag(state);
    }
    if(arg.argc==2)arg.argc=1;
    }
    return rb_ensure(io_s_read, (VALUE) & arg, rb_io_close, arg.io);
}

Note: How to find a method definition location in Ruby

Using RubyMine

In addition, it costs a little money, but with an IDE called RubyMine, a shortcut key will tell you where to define the method.

RubyMine's code jump function is really amazing!!Press Command+B when you're in trouble!

By the way, I use RubyMine.
Command+B helps you find a definition location, which is super convenient.

You should be able to find your method definition location and API documentation in a way that suits you.


2022-09-30 18:28

File.read is the method defined by the parent class IO, so

http://docs.ruby-lang.org/ja/2.1.0/method/IO/s/read.html

available from the .

By the way, you can view a list of parent classes in File.instors.

pry(main)>File.instors
=> [File, IO, File:: Constants, Enumerable, Object, PP:: ObjectMixin, Kernel, BasicObject]


2022-09-30 18:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.