SHARE
TWEET

Untitled

a guest Jul 18th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module TestFunctions
  2.   def abc
  3.     puts 123
  4.   end
  5. end
  6.      
  7. >> TestFunctions.instance_methods
  8. => ["abc"]
  9.      
  10. >> TestFunctions.abc
  11. NoMethodError: undefined method `abc' for TestFunctions:Module from (irb):3
  12.      
  13. >> TestFunctions::abc
  14. NoMethodError: undefined method `abc' for TestFunctions:Module from (irb):4
  15.      
  16. defined?(TestFunctions::abc) #=> nil, but
  17. TestFunctions.method_defined? :abc #=> true
  18.      
  19. module Foo
  20.   def self.method_one
  21.   end
  22.  
  23.   def Foo.method_two
  24.   end
  25.  
  26.   class << self
  27.     def method_three
  28.     end
  29.   end
  30. end
  31.      
  32. module Foo
  33.   def self.method_one
  34.   end
  35.  
  36.   def Foo.method_two
  37.   end
  38.  
  39.   class << self
  40.     def method_three
  41.     end
  42.   end
  43.  
  44.   def method_four
  45.   end
  46.  
  47.   module_function :method_four
  48. end
  49.      
  50. module TestFunctions
  51.   def abc
  52.     puts 123
  53.   end
  54.  
  55.   module_function :abc
  56. end
  57.  
  58. TestFunctions.abc  # => 123
  59.      
  60. module Utilities
  61.  
  62.   def compute_hello(input_string)
  63.      return "Hello #{input_string}"
  64.   end
  65.  
  66. end
  67.      
  68. require "test_helper"
  69. require "utilities"
  70.  
  71. class MyTest < ActiveSupport::TestCase
  72.   include Utilities
  73.  
  74.   def test_compute_hello
  75.     x = compute_hello(input_string="Miles")
  76.     print x
  77.     assert x=="Hello Miles", "Incorrect Response"
  78.   end
  79.  
  80. end
  81.      
  82. >> class MyTest
  83. >>   include TestFunctions
  84. >> end
  85. => MyTest
  86. >> MyTest.new.abc
  87. 123
  88. => nil
  89.      
  90. module TestFunctions
  91.   def TestFunctions.abc
  92.     puts 123
  93.   end
  94. end
  95.      
  96. require 'test_functions'
  97. TestFunctions.abc
  98.      
  99. module TestFunctions
  100.   def abc
  101.     puts 123
  102.   end
  103. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top