Class: Pa::Util
- Inherits:
-
Object
- Object
- Pa::Util
- Defined in:
- lib/pa/util.rb
Overview
In order to reduce the dependencies, this Util class contains some util functions.
Defined Under Namespace
Modules: Concern
Class Method Summary collapse
- .bsd? ⇒ Boolean
-
.extract_options(args, default = {}) ⇒ Array<Array,Hash>
Extracts options from a set of arguments.
-
.join_path(dir, *names) ⇒ Object
different to File.join.
- .linux? ⇒ Boolean
- .mac? ⇒ Boolean
- .posix? ⇒ Boolean
-
.slice(hash, *keys) ⇒ Object
Slice a hash to include only the given keys.
- .solaris? ⇒ Boolean
-
.symbian? ⇒ Boolean
TODO: who knows what symbian returns?.
- .windows? ⇒ Boolean
-
.wrap_array(object) ⇒ Object
Wraps its argument in an array unless it is already an array (or array-like).
Class Method Details
.bsd? ⇒ Boolean
111 112 113 |
# File 'lib/pa/util.rb', line 111 def bsd? RbConfig::CONFIG["host_os"] =~ /bsd/ end |
.extract_options(args, default = {}) ⇒ Array<Array,Hash>
Extracts options from a set of arguments.
24 25 26 27 28 29 30 |
# File 'lib/pa/util.rb', line 24 def (args, default={}) if args.last.instance_of?(Hash) [args[0...-1], default.merge(args[-1])] else [args, default] end end |
.join_path(dir, *names) ⇒ Object
different to File.join.
99 100 101 |
# File 'lib/pa/util.rb', line 99 def join_path(dir, *names) dir == "." ? File.join(*names) : File.join(dir, *names) end |
.linux? ⇒ Boolean
103 104 105 |
# File 'lib/pa/util.rb', line 103 def linux? RbConfig::CONFIG["host_os"] =~ /linux|cygwin/ end |
.mac? ⇒ Boolean
107 108 109 |
# File 'lib/pa/util.rb', line 107 def mac? RbConfig::CONFIG["host_os"] =~ /mac|darwin/ end |
.posix? ⇒ Boolean
128 129 130 131 132 133 134 135 |
# File 'lib/pa/util.rb', line 128 def posix? linux? or mac? or bsd? or solaris? or begin fork do end true rescue NotImplementedError, NoMethodError false end end |
.slice(hash, *keys) ⇒ Object
Slice a hash to include only the given keys. This is useful for limiting an options hash to valid keys before passing to a method:
86 87 88 89 90 91 |
# File 'lib/pa/util.rb', line 86 def slice(hash, *keys) keys = keys.map! { |key| hash.convert_key(key) } if hash.respond_to?(:convert_key) h = hash.class.new keys.each { |k| h[k] = hash[k] if hash.has_key?(k) } h end |
.solaris? ⇒ Boolean
119 120 121 |
# File 'lib/pa/util.rb', line 119 def solaris? RbConfig::CONFIG["host_os"] =~ /solaris|sunos/ end |
.symbian? ⇒ Boolean
TODO: who knows what symbian returns?
124 125 126 |
# File 'lib/pa/util.rb', line 124 def symbian? RbConfig::CONFIG["host_os"] =~ /symbian/ end |
.windows? ⇒ Boolean
115 116 117 |
# File 'lib/pa/util.rb', line 115 def windows? RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ end |
.wrap_array(object) ⇒ Object
Wraps its argument in an array unless it is already an array (or array-like).
Specifically:
-
If the argument is
nil
an empty list is returned. -
Otherwise, if the argument responds to
to_ary
it is invoked, and its result returned. -
Otherwise, returns an array with the argument as its single element.
Array.wrap(nil) # => [] Array.wrap([1, 2, 3]) # => [1, 2, 3] Array.wrap(0) # => [0]
This method is similar in purpose to Kernel#Array
, but there are some differences:
-
If the argument responds to
to_ary
the method is invoked.Kernel#Array
moves on to try to_a
if the returned value is nil
, but Array.wrap
returns such a nil
right away.
-
If the returned value from
to_ary
is neithernil
nor anArray
object,Kernel#Array
raises an exception, while Array.wrap
does not, it just returns the value.
-
It does not call
to_a
on the argument, though special-casesnil
to return an empty array.
The last point is particularly worth comparing for some enumerables:
Array(:foo => :bar) # => [[:foo, :bar]]
Array.wrap(:foo => :bar) # => [{:foo => :bar}]
Array("foo\nbar") # => ["foo\n", "bar"], in Ruby 1.8
Array.wrap("foo\nbar") # => ["foo\nbar"]
There’s also a related idiom that uses the splat operator:
[*object]
which returns [nil]
for nil
, and calls to Array(object)
otherwise.
Thus, in this case the behavior is different for nil
, and the differences with Kernel#Array
explained above apply to the rest of objects.
69 70 71 72 73 74 75 76 77 |
# File 'lib/pa/util.rb', line 69 def wrap_array(object) if object.nil? [] elsif object.respond_to?(:to_ary) object.to_ary || [object] else [object] end end |