File
input = open("examples/aoc/2021/day-1/input").lines()
Literal Specific Methods​
close()​
Returns
BOOLEAN
Closes the file pointer. Returns always true.
content()​
Returns
STRING|ERROR
Reads content of the file and returns it. Resets the position to 0 after read.
lines()​
Returns
ARRAY|ERROR
If successfull, returns all lines of the file as array elements, otherwise nil. Resets the position to 0 after read.
position()​
Returns
INTEGER
Returns the position of the current file handle. -1 if the file is closed.
read(INTEGER)​
Returns
STRING|ERROR
Reads the given amount of bytes from the file. Sets the position to the bytes that where actually read. At the end of file EOF error is returned.
seek(INTEGER, INTEGER)​
Returns
INTEGER|ERROR
Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence. 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end.
write(STRING)​
Returns
INTEGER|ERROR
Writes the given string to the file. Returns number of written bytes on success.
Generic Literal Methods​
is_a?(STRING)​
Returns
BOOLEAN|ERROR
Returns true when the value is of the given type or belongs to the given type group, so one question covers both. The name has to be one that exists: anything else is an error rather than a false, because a typo would otherwise read as a real answer. See Types and type groups.
nil.is_a?("NIL")
"a".is_a?("HASHABLE")
nil.is_a?("HASHABLE")
true.is_a?("INTEGERABLE")
"a".is_a?("INTEGER")
true
true
false
true
false
methods()​
Returns
ARRAY
Returns the names of the methods specific to this literal type, not including the generic methods listed on this page. The names are sorted, so the result is the same on every run. A type with no methods of its own returns an empty array.
1.0.methods().include?("round")
true.methods()
true
[]
nil?()​
Returns
BOOLEAN
Returns true only for nil. Reads better than comparing against nil at the end of a chain, and every type answers it.
nil.nil?()
1.nil?()
"".nil?()
[].first().nil?()
true
false
false
true
to_f()​
Returns
FLOAT|NIL
Converts an object to its float representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0.0.
1.to_f()
"1.4".to_f()
"abc".to_f()
nil.to_f()
1.0
1.4
nil
nil
to_i()​
Returns
INTEGER|NIL
Converts an object to its integer representation, or nil when it cannot. A nil result is what distinguishes a failed conversion from a genuine 0. For strings a 0b, 0o or 0x prefix selects binary, octal or hexadecimal and is matched case insensitively, a leading zero followed only by octal digits is octal, and anything else is decimal. The resulting integer keeps the base it was parsed with, and integers of differing bases cannot be combined directly.
true.to_i()
false.to_i()
1234.to_i()
"4".to_i()
"0".to_i()
"0125".to_i()
"0x2322".to_i()
"0b1010".to_i()
"test".to_i()
1
0
1234
4
0
0o125
0x2322
0b1010
nil
to_json()​
Returns
STRING|ERROR
Returns the object as json notation.
a = {"test": 1234}
a.to_json()
{"test": 1234}
"{\"test\":1234}"
to_s()​
Returns
STRING
Converts an object to its string representation, or the empty string when it has none. Takes no arguments; an integer renders in its own base, so use to_base first to change it.
true.to_s()
1234.to_s()
"test".to_s()
1.4.to_s()
nil.to_s()
"0b1010".to_i().to_s()
"true"
"1234"
"test"
"1.4"
""
"0b1010"
type()​
Returns
STRING
Returns the type of the object.
"test".type()
"STRING"
type_groups()​
Returns
ARRAY
Returns the type groups the value belongs to, sorted. ANY is not listed: every value belongs to it, so it would say nothing while prefixing every answer. It exists for signatures, where push(ANY) means the argument accepts anything, and is_a?("ANY") still answers true. See Types and type groups for what each group means.
1.type_groups()
nil.type_groups()
def() end.type_groups()
puts.type_groups()
["COMPARABLE", "HASHABLE", "INTEGERABLE", "NUMERIC", "STRINGABLE"]
["STRINGABLE"]
["CALLABLE"]
["CALLABLE"]
wat()​
Returns
NIL
Prints the type's literal-specific methods with their argument and return types, sorted by name, one per line. It returns nil rather than the listing: this exists to be read, and the REPL echoes a returned value through its escaped representation, which would put the whole thing on one line. Use methods when the names are wanted as data. A type with no methods of its own prints only the heading.
true.wat()
1.0.wat()
BOOLEAN supports the following methods:
nil
FLOAT supports the following methods:
abs()
ceil([INTEGER])
divmod(FLOAT)
finite?()
floor([INTEGER])
infinite?()
nan?()
negative?()
positive?()
round([INTEGER])
truncate([INTEGER])
zero?()
nil