modulino

Equivalent to Perl Modulino for Ruby, Python?

半腔热情 提交于 2019-12-22 10:24:56
问题 I know Perl has a design pattern known as a modulino, in which a library module file can act as both a library and a script. Is there any equivalent to this in Ruby / Python? I think this design pattern would be very useful for me; I'm writing workers that are fairly short, but also require a script to run them. I think it would be convenient to have this all run from the same place. Thank you! 回答1: Python has __name__ : class MyClass(object): pass if __name__ == '__main__': print("This will

How to include unit tests in a ruby module?

。_饼干妹妹 提交于 2019-12-19 03:19:05
问题 I'm trying to include the unit tests for a module in the same source file as the module itself, following the Perl modulino model. #! /usr/bin/env ruby require 'test/unit' module Modulino def modulino_function return 0 end end class ModulinoTest < Test::Unit::TestCase include Modulino def test_modulino_function assert_equal(0, modulino_function) end end Now, I can run the unit-tests executing this source file. But , they are also run when I require/load them from another script. How can this

How to include unit tests in a ruby module?

ぃ、小莉子 提交于 2019-12-19 03:16:11
问题 I'm trying to include the unit tests for a module in the same source file as the module itself, following the Perl modulino model. #! /usr/bin/env ruby require 'test/unit' module Modulino def modulino_function return 0 end end class ModulinoTest < Test::Unit::TestCase include Modulino def test_modulino_function assert_equal(0, modulino_function) end end Now, I can run the unit-tests executing this source file. But , they are also run when I require/load them from another script. How can this

Should Perl 6 run MAIN if the file is required?

喜欢而已 提交于 2019-12-07 02:53:35
问题 Here's a short Perl 6 program that declare a MAIN subroutine. I should only see output if I execute the program directly: $ cat main.pm6 sub MAIN { say "Called as a program!" } And I see output when I execute the program directly: $ perl6 main.pm6 Called as a program! If I load it as a module, I see no output: $ perl6 -I. -Mmain -e "say 'Hey'" Hey Same if I use it from inside the program, I see no output: $ perl6 -I. -e 'use main' But, if I use require , I get output: $ perl6 -I. -e 'require

Equivalent to Perl Modulino for Ruby, Python?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 19:42:02
I know Perl has a design pattern known as a modulino, in which a library module file can act as both a library and a script. Is there any equivalent to this in Ruby / Python? I think this design pattern would be very useful for me; I'm writing workers that are fairly short, but also require a script to run them. I think it would be convenient to have this all run from the same place. Thank you! Python has __name__ : class MyClass(object): pass if __name__ == '__main__': print("This will only run if you run the script explicitly, not import it") If you run python myscript.py , the print

In Perl, how can I find out if my file is being used as a module or run as a script?

末鹿安然 提交于 2019-11-28 10:04:47
Let's say I have a Perl file in which there are parts I need to run only when I'm called as a script. I remember reading sometime back about including those parts in a main() method and doing a main() unless(<some condition which tests if I'm being used as a module>); But I forgot what the condition was. Searching Google hasn't turned out anything fruitful. Can someone point out the right place to look for this? Sinan Ünür If the file is invoked as a script, there will be no caller so you can use: main() unless caller; See brian d foy 's explanation . #!/usr/bin/perl use strict; use warnings;

In Perl, how can I find out if my file is being used as a module or run as a script?

大兔子大兔子 提交于 2019-11-27 03:23:02
问题 Let's say I have a Perl file in which there are parts I need to run only when I'm called as a script. I remember reading sometime back about including those parts in a main() method and doing a main() unless(<some condition which tests if I'm being used as a module>); But I forgot what the condition was. Searching Google hasn't turned out anything fruitful. Can someone point out the right place to look for this? 回答1: If the file is invoked as a script, there will be no caller so you can use: