Returns the field value as specified by header
.
With the single argument header
, returns the field value for that header (first found):
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row.fetch('Name') # => "Foo"
Raises exception KeyError
if the header does not exist.
With arguments header
and default
given, returns the field value for the header (first found) if the header exists, otherwise returns default
:
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row.fetch('Name', '') # => "Foo" row.fetch(:nosuch, '') # => ""
With argument header
and a block given, returns the field value for the header (first found) if the header exists; otherwise calls the block and returns its return value:
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row.fetch('Name') {|header| fail 'Cannot happen' } # => "Foo" row.fetch(:nosuch) {|header| "Header '#{header} not found'" } # => "Header 'nosuch not found'"
Fetches item k
from the tuple.
Fetches key
from the tuple.
Cached RemoteFetcher
instance.
Default fetcher instance. Use this instead of ::new
to reduce object allocation.
Return value associated with key
.
If there is no value for key
and no block is given, returns ifnone
.
Otherwise, calls block passing in the given key
.
See ::DBM#fetch for more information.
With a block, returns the string value for key
if it exists; otherwise returns the value of the block; ignores the default_val
; see Fields:
res = Net::HTTP.get_response(hostname, '/todos/1') # Field exists; block not called. res.fetch('Connection') do |value| fail 'Cannot happen' end # => "keep-alive" # Field does not exist; block called. res.fetch('Nosuch') do |value| value.downcase end # => "nosuch"
With no block, returns the string value for key
if it exists; otherwise, returns default_val
if it was given; otherwise raises an exception:
res.fetch('Connection', 'Foo') # => "keep-alive" res.fetch('Nosuch', 'Foo') # => "Foo" res.fetch('Nosuch') # Raises KeyError.
Switch the effective and real user IDs of the current process. If a block is given, the user IDs will be switched back after the block is executed. Returns the new effective user ID if called without a block, and the return value of the block if one is given.
Switch the effective and real group IDs of the current process. If a block is given, the group IDs will be switched back after the block is executed. Returns the new effective group ID if called without a block, and the return value of the block if one is given.
Downloads uri
to path
if necessary. If no path is given, it just passes the data.
Returns a relative path from the given base_directory
to the receiver.
If self
is absolute, then base_directory
must be absolute too.
If self
is relative, then base_directory
must be relative too.
This method doesn’t access the filesystem. It assumes no symlinks.
ArgumentError
is raised when it cannot find a relative path.
Note that this method does not handle situations where the case sensitivity of the filesystem in use differs from the operating system default.
Yields each frame of the current execution stack as a backtrace location object.
Returns the class for the given object
.
class A def foo ObjectSpace::trace_object_allocations do obj = Object.new p "#{ObjectSpace::allocation_class_path(obj)}" end end end A.new.foo #=> "Class"
See ::trace_object_allocations
for more information and examples.
Returns the size of memory allocated by malloc().
Only available if ruby was built with CALC_EXACT_MALLOC_SIZE
.
The number of paths in the +$LOAD_PATH+ from activated gems. Used to prioritize -I
and +ENV+ entries during require
.
Returns the value of Gem.source_date_epoch_string
, as a Time
object.
This is used throughout RubyGems for enabling reproducible builds.