Efficiently copy from a source String
into the buffer, at offset
using memcpy
.
buf = IO::Buffer.new(8) # => # #<IO::Buffer 0x0000557412714a20+8 INTERNAL> # 0x00000000 00 00 00 00 00 00 00 00 ........ # set buffer starting from offset 1, take 2 bytes starting from string's # second buf.set_string('test', 1, 2, 1) # => 2 buf # => # #<IO::Buffer 0x0000557412714a20+8 INTERNAL> # 0x00000000 00 65 73 00 00 00 00 00 .es.....
See also copy
for examples of how buffer writing might be used for changing associated strings and files.
Sanitize a single string.
If the SOURCE_DATE_EPOCH environment variable is set, returns it’s value. Otherwise, returns the time that Gem.source_date_epoch_string
was first called in the same format as SOURCE_DATE_EPOCH.
NOTE(@duckinator): The implementation is a tad weird because we want to:
1. Make builds reproducible by default, by having this function always return the same result during a given run. 2. Allow changing ENV['SOURCE_DATE_EPOCH'] at runtime, since multiple tests that set this variable will be run in a single process.
If you simplify this function and a lot of tests fail, that is likely due to #2 above.
Details on SOURCE_DATE_EPOCH: reproducible-builds.org/specs/source-date-epoch/
Returns a status string for the response.
Returns self
if self
is a String
, or self
converted to a String
if self
is a subclass of String
.
Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.
class Fred attr_accessor :a1 def initialize @iv = 3 end end Fred.new.instance_variables #=> [:@iv]
Creates an accessor method to allow assignment to the attribute symbol.id2name
. String
arguments are converted to symbols. Returns an array of defined method names as symbols.
Makes a list of existing constants private.
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :wday
, :yday
.
Possible usages:
d = Date.new(2022, 10, 5) if d in wday: 3, day: ..7 # uses deconstruct_keys underneath puts "first Wednesday of the month" end #=> prints "first Wednesday of the month" case d in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if d in Date(wday: 3, day: ..7) puts "first Wednesday of the month" end
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :wday
, :yday
, :hour
, :min
, :sec
, :sec_fraction
, :zone
.
Possible usages:
dt = DateTime.new(2022, 10, 5, 13, 30) if d in wday: 1..5, hour: 10..18 # uses deconstruct_keys underneath puts "Working time" end #=> prints "Working time" case dt in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if d in DateTime(wday: 1..5, hour: 10..18, day: ..7) puts "Working time, first week of the month" end
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :yday
, :wday
, :hour
, :min
, :sec
, :subsec
, :dst
, :zone
.
Possible usages:
t = Time.utc(2022, 10, 5, 21, 25, 30) if t in wday: 3, day: ..7 # uses deconstruct_keys underneath puts "first Wednesday of the month" end #=> prints "first Wednesday of the month" case t in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if t in Time(wday: 3, day: ..7) puts "first Wednesday of the month" end
Waits until IO
is priority and returns a truthy value or a falsy value when times out. Priority data is sent and received using the Socket::MSG_OOB flag and is typically limited to streams.
You must require ‘io/wait’ to use this method.
Copies from the given src
to the given dst
, returning the number of bytes copied.
The given src
must be one of the following:
The path to a readable file, from which source data is to be read.
An IO-like object, opened for reading and capable of responding to method :readpartial
or method :read
.
The given dst
must be one of the following:
The path to a writable file, to which data is to be written.
An IO-like object, opened for writing and capable of responding to method :write
.
The examples here use file t.txt
as source:
File.read('t.txt') # => "First line\nSecond line\n\nThird line\nFourth line\n" File.read('t.txt').size # => 47
If only arguments src
and dst
are given, the entire source stream is copied:
# Paths. IO.copy_stream('t.txt', 't.tmp') # => 47 # IOs (recall that a File is also an IO). src_io = File.open('t.txt', 'r') # => #<File:t.txt> dst_io = File.open('t.tmp', 'w') # => #<File:t.tmp> IO.copy_stream(src_io, dst_io) # => 47 src_io.close dst_io.close
With argument src_length
a non-negative integer, no more than that many bytes are copied:
IO.copy_stream('t.txt', 't.tmp', 10) # => 10 File.read('t.tmp') # => "First line"
With argument src_offset
also given, the source stream is read beginning at that offset:
IO.copy_stream('t.txt', 't.tmp', 11, 11) # => 11 IO.read('t.tmp') # => "Second line"
Returns a hash of the name/value pairs for the given member names.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.deconstruct_keys([:zip, :address]) h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
Returns all names and values if array_of_names
is nil
:
h = joe.deconstruct_keys(nil) h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). It returns false otherwise.
Returns true for IPv4 multicast address (224.0.0.0/4). It returns false otherwise.
Returns true for IPv6 multicast address (ff00::/8). It returns false otherwise.
Returns a hash of the name/value pairs, to use in pattern matching.
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] distance.deconstruct_keys(nil) #=> {:amount=>10, :unit=>"km"} distance.deconstruct_keys([:amount]) #=> {:amount=>10} # usage case distance in amount:, unit: 'km' # calls #deconstruct_keys underneath puts "It is #{amount} kilometers away" else puts "Don't know how to handle it" end # prints "It is 10 kilometers away"
Or, with checking the class, too:
case distance in Measure(amount:, unit: 'km') puts "It is #{amount} kilometers away" # ... end
Returns a hash of the named captures for the given names.
m = /(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})/.match("18:37:22") m.deconstruct_keys([:hours, :minutes]) # => {:hours => "18", :minutes => "37"} m.deconstruct_keys(nil) # => {:hours => "18", :minutes => "37", :seconds => "22"}
Returns an empty hash if no named captures were defined:
m = /(\d{2}):(\d{2}):(\d{2})/.match("18:37:22") m.deconstruct_keys(nil) # => {}
Compiled source code (String
) on *eval methods on the :script_compiled
event. If loaded from a file, it will return nil.
Compiled instruction sequence represented by a RubyVM::InstructionSequence
instance on the :script_compiled
event.
Note that this method is MRI specific.
Parse a YAML
string in yaml
. Returns the Psych::Nodes::Stream
. This method can handle multiple YAML
documents contained in yaml
. filename
is used in the exception message if a Psych::SyntaxError
is raised.
If a block is given, a Psych::Nodes::Document
node will be yielded to the block as it’s being parsed.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Example:
Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00> Psych.parse_stream("--- a\n--- b") do |node| node # => #<Psych::Nodes::Document:0x00> end begin Psych.parse_stream("--- `", filename: "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
Raises a TypeError
when NilClass
is passed.
See Psych::Nodes
for more information about YAML
AST.