Back in the days, when I was learning Python and did a few projects with it, I realized that I could run Python code directly by running a class' file, without introducing any side effect to the rest of the code that was using that class.
Here is the example
def main(): foo = Base1() foo.base1() if __name__ == "__main__": main()
There is a very nice article that delves into the details, but in short,
__name__ is a special Python variable that contains the name of the currently running script. When the script in question is the current file,
__name__ will evaluate to
(Notice how Python loves
__s. It's almost as if those names were snakes,
creeping into your code.)
How I use it in Ruby (on Rails)
class Page < ApplicationRecord # some ruby code for my model end if __FILE__ == $0 p = Page.find(1) p.testing_something end
And in your terminal, all you have to do to run what's inside that last
statement, is to do:
rails runner app/models/page.rb
Boom. No need for a spec, no need for a rake task. Just one quick and dirty condition, right inside the context of your class. To me, this is quite nice, for those cases when I'm too lazy to take the time to write a full blown test case, but just see if something works, or not.