Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyObtiva/b1c0b4d70a4f3de88ce2ea3cdeecb3dd to your computer and use it in GitHub Desktop.
Save AndyObtiva/b1c0b4d70a4f3de88ce2ea3cdeecb3dd to your computer and use it in GitHub Desktop.
How to test Array Inclusion in Ruby Case Statements via Pattern Matching
# Relating to [YASL](https://github.com/AndyObtiva/yasl)
def dump_ruby_basic_data_type_data(object)
case object.class.ancestors.map(&:name)
in [*, 'Time', *]
object.to_datetime.marshal_dump
in [*, 'Date', *]
object.marshal_dump
in [*, 'Complex', *] | [*, 'Rational', *] | [*, 'Regexp', *] | [*, 'Symbol', *] | [*, 'BigDecimal', *]
object.to_s
in [*, 'Set', *]
object.to_a.uniq.map {|element| dump_structure(element) unless unserializable?(element)}
in [*, 'Range', *]
[object.begin, object.end, object.exclude_end?]
in [*, 'Array', *]
object.map {|element| dump_structure(element) unless unserializable?(element)}
in [*, 'Hash', *]
object.reject do |key, value|
[key, value].detect {|element| unserializable?(element)}
end.map do |pair|
pair.map {|element| dump_structure(element)}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment