Last Updated
Viewed 29 Times
     

I am trying to familiarize myself with modules and this code shows a name clash in main that I can not find any information on how to resolve using a namepace (only) with a module class instance variable. Perhaps not possible?

**Modified code slightly so "c = "MAIN"" is before "include" so no doubt that two c variables exist. Added class so I can specify 'cv::a' which I believe is an example of an explicit receiver as you mentioned. I deliberately created the name collision just to see if I could resolve it (main::c was defined before include in my original code from which this was snipped)

I guess my theoretical question becomes can I in some manner specify an explicit receiver (like M2.C) for the instance variable 'c' defined in a method, the similar to that of 'a' (cv::a) defined in a class.

I had decided probably not possible as I was unable to code a namespace to prefix 'c' but thought to ask thinking I had missed something. If this is possible using my code, could you show me how to do so.*

module M2
def method_vars() @c = "@C"; @d = "@D";  end  
attr :c; attr :d

class Class_var
  def   init()  @a = "A"; @b = "B"; end
  attr :a; attr :b
end
end
c = "MAIN" 
include M2  
method_vars()
puts ">#{c}<--->#{d}<"  # >MAIN<--->@D<
cv = Class_var.new
cv.init()
puts ">#{cv::a}<--->#{cv::b}<" # >A<--->B<  
exit
class Person
  include Voice
  include Beep
end

module Voice
  def speak
    puts 'speaking from module'
  end

  def original_speak
    # ???
  end
end

module Beep
  def speak
    puts 'beep beep'
  end
end

How to call Voice#speak? For example:

Person.new.voice_speak

I want to add this code to the Voice module, not the Person class or the Beep module.

Use case:
A module that does soft delete:

module Undeletable
  def delete
    # mark document as deleted. This creates a deletion document.
  end

  def restore
    # Delete (for real) the deletion.
  end

  def obliterate
    restore # because we don't want orphaned deletions.

    real_delete # This should call Mongoid#delete
  end
end

def Foo
  include Mongoid::Document
  include Undeletable
end

So, in general, when we call foo.delete, we want to soft delete. However, in rare cases, we want to do a real delete. The module should support both methods.

Have a look at the code below

initshared.rb
module InitShared
  def init_shared
    @shared_obj = "foobar"
  end
end

myclass.rb

class MyClass
  def initialize()
  end
  def init
    file_name = Dir.pwd+"/initshared.rb"
    if File.file?(file_name)
      require file_name
      include InitShared
      if self.respond_to?'init_shared'
        init_shared
        puts @shared_obj
      end
    end
  end
end

The include InitShared dosn't work since its inside the method .

I want to check for the file and then include the module and then access the variables in that module.

Similar Question 3 : Ruby using namespace/module

For example:

require 'net/http'
uri = URI('http://example.com/some_path?query=string')

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri

  response = http.request request # Net::HTTPResponse object
end

What is the correct/rubist way to get rid of Net::HTTP ? i.e. HTTP::Get.new() or just Get.new()

Similar Question 5 (1 solutions) : How to call a method defined in another module in Ruby?

Similar Question 8 (2 solutions) : Ruby: Use module method inside a class method

cc