lib/lite/ruby/object.rb in lite-ruby-1.0.3 vs lib/lite/ruby/object.rb in lite-ruby-1.0.4
- old
+ new
@@ -1,14 +1,14 @@
# frozen_string_literal: true
class Object
- FALSE_VALUES ||= [
- false, 0, '0', 'false', 'FALSE', 'f', 'F'
+ FALSE_VALUES ||= %w[
+ 0 f false n no off
].freeze
- TRUE_VALUES ||= [
- true, 1, '1', 'true', 'TRUE', 't', 'T'
+ TRUE_VALUES ||= %w[
+ 1 t true y yes on
].freeze
def array?
is_a?(Array)
end
@@ -24,11 +24,12 @@
def bool?
true? || false?
end
def boolean?
- TRUE_VALUES.include?(self) || FALSE_VALUES.include?(self)
+ val = to_s.downcase
+ TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val)
end
def date?
is_a?(Date)
end
@@ -38,11 +39,11 @@
false == self
end
# rubocop:enable Style/YodaCondition
def falsey?
- nil? || FALSE_VALUES.include?(self)
+ nil? || FALSE_VALUES.include?(to_s.downcase)
end
def float?
is_a?(Float)
end
@@ -53,10 +54,16 @@
def integer?
is_a?(Integer)
end
+ # rubocop:disable Naming/PredicateName
+ def is_any?(*objs)
+ objs.any? { |obj| is_a?(obj) }
+ end
+ # rubocop:enable Naming/PredicateName
+
def numeral?
!to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
end
def numeric?
@@ -127,18 +134,27 @@
def time?
is_a?(Time)
end
+ def to_bool
+ return true if truthy?
+ return false if falsey?
+
+ nil
+ end
+
+ alias to_b to_bool
+
# rubocop:disable Style/YodaCondition
def true?
true == self
end
# rubocop:enable Style/YodaCondition
def truthy?
- TRUE_VALUES.include?(self)
+ TRUE_VALUES.include?(to_s.downcase)
end
def try(*obj, &block)
try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
end
@@ -159,24 +175,8 @@
def try_send(*keys)
send(*keys)
rescue StandardError
nil
- end
-
-end
-
-class FalseClass
-
- def to_i
- 0
- end
-
-end
-
-class TrueClass
-
- def to_i
- 1
end
end