Skip to content

Instantly share code, notes, and snippets.

@philk
Created July 12, 2012 17:33
Show Gist options
  • Select an option

  • Save philk/3099524 to your computer and use it in GitHub Desktop.

Select an option

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)
class Object
def to_py_bool
(!!self).to_s.capitalize
end
end
@jordane
Copy link

jordane commented Jul 12, 2012

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 !!self instead of just self?

@philk
Copy link
Author

philk commented Jul 12, 2012

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".

@jordane
Copy link

jordane commented Jul 12, 2012

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!

@philk
Copy link
Author

philk commented Jul 12, 2012

Fry

@jordane
Copy link

jordane commented Jul 12, 2012

zoidberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment