Last active
December 1, 2017 22:46
-
-
Save rjlynch/c231705a6f4276d2acf04fe346583ee3 to your computer and use it in GitHub Desktop.
inspected_rails_object_to_hash.rb
This file contains hidden or 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
| target = '#<Workout id: 9, name: "FFF", created_at: "2017-07-08 22:49:20", updated_at: "2017-07-08 22:49:20", date: "2017-07-08 00:00:00", user_id: 1>' | |
| captures = target.scan /\s(\w+):(?:\s"|\s)(.*?)(?=(?:",\s|,\s|">$|>$))/ #=> [["id", "9"], ["name", "FFF"], ["created_at", "2017-07-08 22:49:20"], ["updated_at", "2017-07-08 22:49:20"], ["date", "2017-07-08 00:00:00"], ["user_id", "1"]] | |
| Hash[captures] #=> {"id"=>"9", "name"=>"FFF", "created_at"=>"2017-07-08 22:49:20", "updated_at"=>"2017-07-08 22:49:20", "date"=>"2017-07-08 00:00:00", "user_id"=>"1"} | |
| # Regex break down | |
| # a space followed by one or more words, that we capture | |
| # followed by a colon | |
| # followed by a space and quote or just a space, neither of which we capture | |
| # followed by zero or more of any character, which we capture | |
| # upto one of the following sequences, that we dont capture; | |
| # a quote comma space | |
| # or comma space | |
| # or quote right angle end of line | |
| # or right angle end of line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment