Skip to content

Instantly share code, notes, and snippets.

@viniciusteles
Created May 13, 2010 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viniciusteles/399401 to your computer and use it in GitHub Desktop.
Save viniciusteles/399401 to your computer and use it in GitHub Desktop.
# Returns a copy of self with all +true+ elements removed.
# [ "a", true, "b", true, "c", true].compact_true #=> [ "a", "b", "c" ]
#
def compact_true
remove_elements(TrueClass)
end
# Returns a copy of self with all +false+ elements removed.
# [ "a", false, "b", false, "c", false].compact_true #=> ["a","b","c" ]
#
def compact_false
remove_elements(FalseClass)
end
private
def remove_elements(boolean_class)
self.clone.delete_if { |element| element.is_a?(boolean_class) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment