Module: Blankity::To

Defined in:
lib/blankity/to.rb

Overview

Convenience methods for creating new BlankIty::ToXXX instances.

This module can be include/+extend+ed to define the ‘to` function, which lets you do things like system to.str(’ls -al’). (The module is not mixin-able, as it overwrites Kernel methods, notably proc and hash).

Class Method Summary collapse

Class Method Details

.a(*elements) ⇒ Object

Convenience method to make Blankity::ToAs from elements



39
# File 'lib/blankity/to.rb', line 39

def self.a(*elements, **, &) = ToA.new(elements, **, &)

.ary(*elements) ⇒ Object

Convenience method to make Blankity::ToArys from elements



44
# File 'lib/blankity/to.rb', line 44

def self.ary(*elements, **, &) = ToAry.new(elements, **, &)

.c(value) ⇒ Object

Convenience method to make Blankity::ToCs from value.to_c



91
# File 'lib/blankity/to.rb', line 91

def self.c(value, ...) = ToC.new(value.to_c, ...)

.f(value) ⇒ Object

Convenience method to make Blankity::ToFs from value.to_f



96
# File 'lib/blankity/to.rb', line 96

def self.f(value, ...) = ToF.new(value.to_f, ...)

.h(hash = nil) ⇒ Object

Convenience method to make Blankity::ToHs from hash

This supports passing in key/values directly via Blankity::To.h(‘a’ => ‘b’) as a convenient shorthand, but you can’t then pass keyword arguments to Blankity::ToH‘s constructor. To do so, instead pass in a Hash as a positional argument (e.g. Blankity::To.h({ ’a’ => ‘b’ }, …))



54
55
56
57
58
59
60
# File 'lib/blankity/to.rb', line 54

def self.h(hash = nil, **, &)
  if hash
    ToH.new(hash.to_h, **, &)
  else
    ToH.new({**}, &)
  end
end

.hash(hash = nil) ⇒ Object

Convenience method to make Blankity::ToHashs from hash

This supports passing in key/values directly via Blankity::To.hash(‘a’ => ‘b’) as a convenient shorthand, but you can’t then pass keyword arguments to Blankity::ToHash‘s constructor. To do so, instead pass in a Hash as a positional argument (e.g. Blankity::To.hash({ ’a’ => ‘b’ }, …))



70
71
72
73
74
75
76
# File 'lib/blankity/to.rb', line 70

def self.hash(hash = nil, **, &)
  if hash
    ToHash.new(hash.to_hash, **, &)
  else
    ToHash.new({**}, &)
  end
end

.i(value) ⇒ Object

Convenience method to make Blankity::ToIs from value.to_i



19
# File 'lib/blankity/to.rb', line 19

def self.i(value, ...) = ToI.new(value.to_i, ...)

.int(value) ⇒ Object

Convenience method to make Blankity::ToInts from value.to_int



24
# File 'lib/blankity/to.rb', line 24

def self.int(value, ...) = ToInt.new(value.to_int, ...)

.io(value) ⇒ Object

Convenience method to make Blankity::ToIOs from value.to_io



112
# File 'lib/blankity/to.rb', line 112

def self.io(value, ...) = ToIO.new(value.to_io, ...)

.path(value) ⇒ Object

Convenience method to make Blankity::ToPaths from value.to_path, or Kernel#String(value) if value doesn’t define #to_path.



107
# File 'lib/blankity/to.rb', line 107

def self.path(value, ...) = ToPath.new(defined?(value.to_path) ? value.to_path : String(value), ...)

.proc(proc = nil, &block) ⇒ Object

Convenience method to make Blankity::ToProcs from the supplied block, or proc if no block is given.

This supports passing blocks in directly via Blankity::To.proc { … } as a convenient shorthand, but then you can’t pass a block to Blankity::ToProc‘s constructor. To so do, instead pass the block as a positional parameter (eg Blankity::To.proc(proc { … }) { … })



122
123
124
125
126
127
128
129
130
# File 'lib/blankity/to.rb', line 122

def self.proc(proc = nil, **, &block)
  if proc
    ToProc.new(proc.to_proc, **, &block)
  elsif !block_given?
    raise ArgumentError, 'if an explicit proc is omitted, a block must be passed'
  else
    ToProc.new(__any__ = block, **)
  end
end

.r(value) ⇒ Object

Convenience method to make Blankity::ToRs from value.to_r



86
# File 'lib/blankity/to.rb', line 86

def self.r(value, ...) = ToR.new(value.to_r, ...)

.range(begin_, end_, exclude_end = false) ⇒ Object

Convenience method to make Ranges from the supplied arguments.



135
136
137
# File 'lib/blankity/to.rb', line 135

def self.range(begin_, end_, exclude_end = false, ...)
  __any__ = Range.new(begin_, end_, exclude_end, ...)
end

.regexp(value) ⇒ Object

Convenience method to make Blankity::ToRegexps from value.to_regexp



101
# File 'lib/blankity/to.rb', line 101

def self.regexp(value, ...) = ToRegexp.new(Regexp === value ? value : value.to_regexp, ...)

.s(value) ⇒ Object

Convenience method to make Blankity::ToSs from value.to_s



29
# File 'lib/blankity/to.rb', line 29

def self.s(value, ...) = ToS.new(value.to_s, ...)

.str(value) ⇒ Object

Convenience method to make Blankity::ToStrs from value.to_str



34
# File 'lib/blankity/to.rb', line 34

def self.str(value, ...) = ToStr.new(value.to_str, ...)

.sym(value) ⇒ Object

Convenience method to make Blankity::ToSyms from value.to_sym



81
# File 'lib/blankity/to.rb', line 81

def self.sym(value, ...) = ToSym.new(value.to_sym, ...)

.toObject

Helper method so you can ‘include Blankity::To` and then `to.str(…)`.



14
# File 'lib/blankity/to.rb', line 14

module_function def to = ::Blankity::To