Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Array#none check if already defined

  • Loading branch information...
commit ba1f74aca966e1e46cb75d0cb10aeb99ba64ca7b 1 parent ee741b8
@kristianmandrup authored
View
4 lib/sugar-high/array.rb
@@ -90,7 +90,7 @@ def to_files!
def none?
self.flatten.compact.empty?
- end
+ end unless [].respond_to? :none?
@sj26
sj26 added a note

This actually doesn't do the same thing as Enumerable#none?, might be confusing to seemingly polyfill with a different purpose.

It seems what you want to express here is similar to Object#blank? as provided by activesupport, but recursively. You could use [[], []].all? &:blank or maybe use:

module Enumerable
  def all_blank?
    all? { |e| if e.respond_to? :all_blank? then e.all_blank? else e.blank? end }
  end
end

Maybe even a helper which lets Enumerable#all? work recursively in a generic way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def flat_uniq
self.flatten.compact.uniq
@@ -110,7 +110,7 @@ def extract(sym)
# Repeat overall-used method here, to use it without Rails
def extract_options!
last.is_a?(::Hash) ? pop : {}
- end unless defined? Array.new.extract_options!
+ end unless [].respond_to? :extract_options!
end
module MathArray
View
10 lib/sugar-high/blank.rb
@@ -25,12 +25,10 @@ class Hash #:nodoc:
alias_method :blank?, :empty?
end
-class String
- unless "".respond_to? :blank
- def blank?
- self !~ /\S/
- end
- end
+class String
+ def blank?
+ self !~ /\S/
+ end unless "".respond_to? :blank
end
View
21 lib/sugar-high/string.rb
@@ -10,20 +10,17 @@ def self.letters type = :lower
type == :upper ? letters.map!(&:upcase) : letters
end
- unless "".respond_to? :trim
- def trim
- self.strip
- end
- end
+ def trim
+ self.strip
+ end unless "".respond_to? :trim
- unless "".respond_to? :concats
- def concats *args
- args.inject(self) do |res, arg|
- res << arg.to_s
- res
- end
+
+ def concats *args
+ args.inject(self) do |res, arg|
+ res << arg.to_s
+ res
end
- end
+ end unless "".respond_to? :concats
# remove prefixed '-' signs, then allow any letter, number, underscore '_' or dash '-'
def alpha_numeric
Please sign in to comment.
Something went wrong with that request. Please try again.