Files
mruby-mruby/mrbgems/mruby-io/mrblib/kernel.rb
T
Yukihiro "Matz" Matsumoto 7b9d1da3fc mruby-io: add comprehensive call-seq documentation for all Ruby and C methods
Added complete call-seq documentation for the entire mruby-io gem across
both Ruby and C implementations:

## Ruby Methods (mrblib/) - 50 methods documented:

### Kernel Module (kernel.rb):
- Backtick operator: shell command execution with output capture
- open: unified file/subprocess opening with pipe support
- p: debug output with inspect formatting and multiple argument handling
- print/puts/printf: output methods with proper formatting and separators
- gets/readline/readlines: input methods with various line handling options

### File Constants (file_constants.rb):
- FNM_* constants: file name matching flags for glob and fnmatch operations
  with detailed explanations of case sensitivity, escaping, and pattern behavior

### IO Class (io.rb):
Class methods:
- IO.open: creates IO objects with automatic resource management
- IO.popen: subprocess communication with pipe handling
- IO.pipe: creates connected pipe endpoints for IPC
- IO.read: convenience method for reading entire files

Instance methods:
- Stream positioning: pos=, rewind, tell with proper seeking behavior
- Iteration: each, each_byte, each_char with enumerator support
- Output: puts, print, printf with formatting and newline handling
- Utility: hash, <<, ungetbyte with proper stream manipulation
- Global streams: STDIN/STDOUT/STDERR and $stdin/$stdout/$stderr

### File Class (file.rb):
Instance methods:
- Constructor: handles both file paths and file descriptors
- Timestamps: atime, ctime, mtime with proper Time object conversion
- Inspection: inspect method for debugging file objects

Class methods:
- Path utilities: join with cross-platform separator handling
- File iteration: foreach with block and enumerator support
- FileTest delegation: complete set of file type and existence checks
  (directory?, exist?, file?, pipe?, size, socket?, symlink?, zero?)
- Path manipulation: extname for extension extraction, path for conversion

## C Methods (src/) - 25 methods documented:

### Core IO Operations (io.c):
- File descriptor management: fileno with proper error handling
- Stream state: closed?, eof?, sync/sync= for buffering control
- Process management: pid for pipe process tracking
- Resource management: close_on_exec?/close_on_exec= for FD_CLOEXEC handling

### Reading Operations:
- Character reading: getc, readchar with EOF handling differences
- Byte reading: getbyte, readbyte with integer conversion
- Buffer reading: read with length and output buffer support
- Stream manipulation: ungetc for character pushback

### System Operations:
- IO multiplexing: IO.select for monitoring multiple streams
- Constructor: IO.new for creating IO objects from file descriptors
- Stream flushing: flush for forcing output to OS

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:48 +09:00

162 lines
3.9 KiB
Ruby

module Kernel
#
# call-seq:
# `cmd` -> string
#
# Returns the standard output of running cmd in a subshell.
# The built-in syntax %x{...} uses this method.
#
# `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
# `ls testdir`.split[1] #=> "main.rb"
# `echo oops && exit 99` #=> "oops\n"
#
private def `(cmd) #`
IO.popen(cmd) { |io| io.read }
end
#
# call-seq:
# open(name [, mode [, perm]] [, opt]) -> io or nil
# open(name [, mode [, perm]] [, opt]) {|io| block } -> obj
#
# Creates an IO object connected to the given stream, file, or subprocess.
# If path starts with a pipe character ("|"), a subprocess is created,
# connected to the caller by a pair of pipes. The returned IO object may
# be used to write to the standard input and read from the standard output
# of this subprocess.
#
# open("testfile") #=> #<File:testfile>
# open("| date") #=> #<IO:fd 6>
# open("testfile") do |f|
# print f.gets
# end
#
private def open(file, *rest, &block)
raise ArgumentError unless file.is_a?(String)
if file[0] == "|"
IO.popen(file[1..-1], *rest, &block)
else
File.open(file, *rest, &block)
end
end
#
# call-seq:
# p(obj) -> obj
# p(obj1, obj2, ...) -> [obj1, obj2, ...]
# p() -> nil
#
# For each object, directly writes obj.inspect followed by a newline
# to the program's standard output.
#
# S = Struct.new(:name, :state)
# s = S['dave', 'TX']
# p s #=> #<struct S name="dave", state="TX">
#
private def p(*a)
for e in a
$stdout.write e.inspect
$stdout.write "\n"
end
len = a.size
return nil if len == 0
return a[0] if len == 1
a
end
#
# call-seq:
# print(obj, ...) -> nil
#
# Prints each object in turn to $stdout. If the output field separator
# ($,) is not nil, its contents will appear between each field.
# If the output record separator ($\) is not nil, it will be appended
# to the output.
#
# print "cat", [1,2,3], 99, "\n"
# $, = ", "
# $\ = "\n"
# print "cat", [1,2,3], 99
#
private def print(...)
$stdout.print(...)
end
#
# call-seq:
# puts(obj, ...) -> nil
#
# Equivalent to $stdout.puts(obj, ...).
#
# puts "this", "is", "a", "test"
#
private def puts(...)
$stdout.puts(...)
end
#
# call-seq:
# printf(io, string [, obj ... ]) -> nil
# printf(string [, obj ... ]) -> nil
#
# Equivalent to io.write(sprintf(string, obj, ...)) or
# $stdout.write(sprintf(string, obj, ...)).
#
# printf "Number: %5.2f,\nString: %s\n", 1.23, "hello"
#
private def printf(...)
$stdout.printf(...)
end
#
# call-seq:
# gets(sep=$/) -> string or nil
# gets(limit) -> string or nil
# gets(sep,limit) -> string or nil
#
# Returns (and assigns to $_) the next line from the list of files in ARGV
# (or $*), or from standard input if no files are present on the command line.
# Returns nil at end of file.
#
# print "Enter your name: "
# name = gets
# print "Hello #{name}"
#
private def gets(...)
$stdin.gets(...)
end
#
# call-seq:
# readline(sep=$/) -> string
# readline(limit) -> string
# readline(sep,limit) -> string
#
# Equivalent to gets, except readline raises EOFError at end of file.
#
# print "Enter your name: "
# name = readline
# print "Hello #{name}"
#
private def readline(...)
$stdin.readline(...)
end
#
# call-seq:
# readlines(sep=$/) -> array
# readlines(limit) -> array
# readlines(sep,limit) -> array
#
# Returns an array containing the lines returned by calling gets(sep)
# until the end of file.
#
# lines = readlines
# lines[0] #=> "This is line one\n"
#
private def readlines(...)
$stdin.readlines(...)
end
end