Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Question about send

hy a ask if i can do this:

Code:
module Addon
  Parts=[]
  def self.set(item)
    Parts.each { |name| send(name).set(item) if send(name).respond_to?(:set) }
  end
end

module Addon
 Parts << "A"
 module A
  def self.set(item)
   #do
  end
 end
end

module Addon
 Parts << "B"
 module B
  def self.set(item)
   #do
  end
 end
end

module Addon
 Parts << "C"
 module C
  def self.set(item)
   #do
  end
 end
end

i should start the set funktions of the Parts, but you can leave Parts out.
 

khmp

Sponsor

Like a plugin system? It's very interesting code to read. Should the member operator, "." between send and the Parts piece be the scope resolution operator, "::" because you are dealing with modules?
 
Code:
obj.send(symbol, *args, &block)

You are envoking a method, identified by the symbol. Because you are envoking a method, you need to send required args and block, if needed.

Code:
class Something
  def initialize
    @a = 5
  end
  def add_x(x = 3)
    @a += x
    return @a
  end
end

something = Something.new
something.send(:add_x, 1) #=> 6
something.send(:add_x, 5) #=> 11
 
@SephirothSpawn i now that code for defs but i ask if it funtion with modules.

sample: you have A and B
start with Addon.set(item)
run:
->A.set(item)
->B.set(item)

sample: you have B and C
start with Addon.set(item)
run:
->B.set(item)
->C.set(item)

sample: you have B and D, but D have no set metode
start with Addon.set(item)
run:
->B.set(item)

can you understand me?
 

khmp

Sponsor

Code:
module Addon
  Debug_Output = true
  Parts=[]
  def self.set(item)
    Parts.each { |name| name::send(:set, item) if name::respond_to?(:set) }
  end
end

module Addon
  module A
    def self.set(item)
      p 'A::set(item) was executed' if Debug_Output
      #do
    end
  end
  Parts << A
end

module Addon
  module B
    def self.set(item)
      p 'B::set(item) was executed' if Debug_Output
      #do
    end
  end
  Parts << B
end

module Addon
  module C
    def self.set(item)
      p 'C::set(item) was executed' if Debug_Output
      #do
    end
  end
  Parts << C
end

Ok I'll try to explain as best I can where you went wrong.
Code:
Parts.each { |name| send(name).set(item) if send(name).respond_to?(:set) }
The method 'send' needs an invoking object. Otherwise Ruby believes you're trying to send information via socketing. Like SephirothSpawn has above about send. It works by passing a symbol as the first argument and then the arguments that go into the method normally. It adds a level of complexity to the code in my opinion using send instead of just calling the method. I'd imagine if you needed to use it like a function pointer it would be useful. So I hope that answers your questions about using send.

When you added "strings" into the array, Parts, the runtime execution was struggling how to interpret the string as a module. So instead I changed it so it just adds the actual module into Parts instead. You'll notice that I moved it below the module from where it was. The module must be defined before it is added into Parts, it can not be done before. Lastly, like I said above '::' scope resolution operator because we are dealing with Ruby modules, and not the '.' member operator for dealing with classes.

Good luck with it hanmac! :thumb:
 
Just use something like this:
Code:
module Addon
  Parts = []
  def self.set(item)
    Parts.each {|s| s.set(item) if s.respond_to?(:set)
  end
end

module Addon
  module A
    Addon::Parts << self
    def self.set(item)
      p 'a'
    end
  end
end

Just send the entire module, not a letter representation.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top