Created
January 1, 2021 01:15
-
-
Save AndyObtiva/b1c0b4d70a4f3de88ce2ea3cdeecb3dd to your computer and use it in GitHub Desktop.
How to test Array Inclusion in Ruby Case Statements via Pattern Matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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