Returns the byte at zero-based index
as an integer:
s = 'abcde' # => "abcde" s.getbyte(0) # => 97 s.getbyte(1) # => 98
Related: String#setbyte
.
Sets the byte at zero-based index
to integer
; returns integer
:
s = 'abcde' # => "abcde" s.setbyte(0, 98) # => 98 s # => "bbcde"
Related: String#getbyte
.
For the given method names, marks the method as passing keywords through a normal argument splat. This should only be called on methods that accept an argument splat (*args
) but not explicit keywords or a keyword splat. It marks the method such that if the method is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the method to other methods.
This should only be used for methods that delegate keywords to another method, and only for backwards compatibility with Ruby versions before 3.0. See www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/ for details on why ruby2_keywords
exists and when and how to use it.
This method will probably be removed at some point, as it exists only for backwards compatibility. As it does not exist in Ruby versions before 2.7, check that the module responds to this method before calling it:
module Mod def foo(meth, *args, &block) send(:"do_#{meth}", *args, &block) end ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true) end
However, be aware that if the ruby2_keywords
method is removed, the behavior of the foo
method using the above approach will change so that the method does not pass through keywords.
Gets the next 8-bit byte (0..255) from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getbyte #=> 84 f.getbyte #=> 104
Reads a byte as with IO#getbyte
, but raises an EOFError
on end of file.
Pushes back bytes (passed as a parameter) onto ios, such that a subsequent buffered read will return it. It is only guaranteed to support a single byte, and only if ungetbyte or ungetc has not already been called on ios since the previous read of at least a single byte from ios. However, it can support additional bytes if there is space in the internal buffer to allow for it.
f = File.new("testfile") #=> #<File:testfile> b = f.getbyte #=> 0x38 f.ungetbyte(b) #=> nil f.getbyte #=> 0x38
If given an integer, only uses the lower 8 bits of the integer as the byte to push.
f = File.new("testfile") #=> #<File:testfile> f.ungetbyte(0x102) #=> nil f.getbyte #=> 0x2
Calling this method prepends to the existing buffer, even if the method has already been called previously:
f = File.new("testfile") #=> #<File:testfile> f.ungetbyte("ab") #=> nil f.ungetbyte("cd") #=> nil f.read(5) #=> "cdab8"
Has no effect with unbuffered reads (such as IO#sysread
).
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for hostname.
p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
Use Addrinfo#getnameinfo
instead. This method is deprecated for the following reasons:
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyaddr() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for address.
p Socket.gethostbyaddr([221,186,184,68].pack("CCCC")) #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"] p Socket.gethostbyaddr([127,0,0,1].pack("CCCC")) ["localhost", [], 2, "\x7F\x00\x00\x01"] p Socket.gethostbyaddr(([0]*15+[1]).pack("C"*16)) #=> ["localhost", ["ip6-localhost", "ip6-loopback"], 10, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"]
Obtains the port number for service_name.
If protocol_name is not given, “tcp” is assumed.
Socket.getservbyname("smtp") #=> 25 Socket.getservbyname("shell") #=> 514 Socket.getservbyname("syslog", "udp") #=> 514
Obtains the port number for port.
If protocol_name is not given, “tcp” is assumed.
Socket.getservbyport(80) #=> "www" Socket.getservbyport(514, "tcp") #=> "shell" Socket.getservbyport(514, "udp") #=> "syslog"
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method lookups host information by hostname.
TCPSocket.gethostbyname("localhost") #=> ["localhost", ["hal"], 2, "127.0.0.1"]
Checks if a given hash is flagged by Module#ruby2_keywords
(or Proc#ruby2_keywords
). This method is not for casual use; debugging, researching, and some truly necessary cases like serialization of arguments.
ruby2_keywords def foo(*args) Hash.ruby2_keywords_hash?(args.last) end foo(k: 1) #=> true foo({k: 1}) #=> false
Duplicates a given hash and adds a ruby2_keywords flag. This method is not for casual use; debugging, researching, and some truly necessary cases like deserialization of arguments.
h = {k: 1} h = Hash.ruby2_keywords_hash(h) def foo(k: 42) k end foo(*[h]) #=> 1 with neither a warning or an error
Gets the next 8-bit byte (0..255) from ARGF
. Returns nil
if called at the end of the stream.
For example:
$ echo "foo" > file $ ruby argf.rb file ARGF.getbyte #=> 102 ARGF.getbyte #=> 111 ARGF.getbyte #=> 111 ARGF.getbyte #=> 10 ARGF.getbyte #=> nil
Reads the next 8-bit byte from ARGF
and returns it as an Integer
. Raises an EOFError
after the last byte of the last file has been read.
For example:
$ echo "foo" > file $ ruby argf.rb file ARGF.readbyte #=> 102 ARGF.readbyte #=> 111 ARGF.readbyte #=> 111 ARGF.readbyte #=> 10 ARGF.readbyte #=> end of file reached (EOFError)
Marks the proc as passing keywords through a normal argument splat. This should only be called on procs that accept an argument splat (*args
) but not explicit keywords or a keyword splat. It marks the proc such that if the proc is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the proc to other methods.
This should only be used for procs that delegate keywords to another method, and only for backwards compatibility with Ruby versions before 2.7.
This method will probably be removed at some point, as it exists only for backwards compatibility. As it does not exist in Ruby versions before 2.7, check that the proc responds to this method before calling it. Also, be aware that if this method is removed, the behavior of the proc will change so that it does not pass through keywords.
module Mod foo = ->(meth, *args, &block) do send(:"do_#{meth}", *args, &block) end foo.ruby2_keywords if foo.respond_to?(:ruby2_keywords) end
Returns the version of libyaml being used
The path to the running Ruby interpreter.
Returns a String
containing the API compatibility version of Ruby
Returns the latest release version of RubyGems.
A Gem::Version
for the currently running Ruby.
A Gem::Version
for the currently running RubyGems