Created
July 12, 2012 17:33
-
-
Save philk/3099524 to your computer and use it in GitHub Desktop.
Convert any Ruby object into a Python string (for ERB templates in Chef)
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
| class Object | |
| def to_py_bool | |
| (!!self).to_s.capitalize | |
| end | |
| end |
Author
So !self returns the opposite either TrueClass or FalseClass as the literal class based on Ruby's true/false rules (meaning anything except 0, nil, and false returns true). !!self returns the original TrueClass or FalseClass as the literal class. From there you can be guaranteed that the thing that you're .to_sing is going to be the actual String "true" or "false".
So what you're saying is that after adding this, we can further obscure our attributes by assigning them differing non-boolean values, and yet they will all end up as the right thing. +1!
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Guess I'll add this comment here instead of my closed PR:
Maybe I'm crazy, but ! is the boolean negation operator in ruby, right? So why call
!!selfinstead of justself?