Block issues

In the following block, each plugin in the constant hash PLUGINS is
supposed to take two parameters, server and config. The only problem
here is that:

rmss.rb:175:in start_plugins': undefined methodplugin’ for
main:Object (NoMethodError)

def start_plugins(server)
plugins = []
PLUGINS.each { |plugin|
if CONFIG.has_key?(plugin.IDENTIFIER)
plugins.append(plugin(server, CONFIG[plugin.IDENTIFIER]))
end
}
end

Does anyone have any solution for this?

The idea is to load a list of plugins from the PLUGINS and CONFIG hashes
into an array, with their matching configuration, and then run them all.
The latter part is not included in this code, as is also the case for
when there is no configuration, which has also not been added.

Hi –

On Thu, 17 Sep 2009, Dylan Lukes wrote:

if CONFIG.has_key?(plugin.IDENTIFIER)
The idea is to load a list of plugins from the PLUGINS and CONFIG hashes
into an array, with their matching configuration, and then run them all.
The latter part is not included in this code, as is also the case for
when there is no configuration, which has also not been added.

If PLUGINS is a hash, then you probably want to do something like:

PLUGINS.each {|key, value| … }

But the main problem is that you’re using a variable name as a method
name, and apparently there’s no such method. So the solution will
depend on exactly what method you’re really trying to run. What
exactly is in the hash?

David

Dylan Lukes wrote:

In the following block, each plugin in the constant hash PLUGINS is
supposed to take two parameters, server and config. The only problem
here is that:

rmss.rb:175:in start_plugins': undefined methodplugin’ for
main:Object (NoMethodError)

def start_plugins(server)
plugins = []
PLUGINS.each { |plugin|
if CONFIG.has_key?(plugin.IDENTIFIER)
plugins.append(plugin(server, CONFIG[plugin.IDENTIFIER]))
end
}
end

Does anyone have any solution for this?

David A. Black wrote:

But the main problem is that…

You’ve named every variable in your program “plugin”. Guess what?
That’s confusing.

You’re specific error is in this line:

  plugins.append(plugin(server, CONFIG[plugin.IDENTIFIER]))

See the stuff in the parentheses? Specifically this:

plugin(server, CONFIG[plugin.IDENTIFIER])

Where is the plugin() method?

7stud – wrote:

Where is the plugin() method?

ruby is telling you, “Hey guy who names every variable ‘plugin’!
There’s no plugin method defined anywhere.”

Hi,

Am Donnerstag, 17. Sep 2009, 09:19:08 +0900 schrieb 7stud --:

You’ve named every variable in your program “plugin”. Guess what?
That’s confusing.

By the way: I find myself mixing singular and plural all the time.
I keep discipline with arg' andargs’:

def meth *args
args.each { |arg|
arg.do_sth
}
end

But when using hashes I name the variable without concern:

pets = {}
pets[ :dog] = “gluttonous”

or

pet = {}
pet[ :dog] = “gluttonous”

What do you think is the correct way to cope?

Bertram

Bertram S. wrote:

Hi,

Am Donnerstag, 17. Sep 2009, 09:19:08 +0900 schrieb 7stud --:

You’ve named every variable in your program “plugin”. Guess what?
That’s confusing.

By the way: I find myself mixing singular and plural all the time.
I keep discipline with arg' andargs’:

def meth *args
args.each { |arg|
arg.do_sth
}
end

But when using hashes I name the variable without concern:

pets = {}
pets[ :dog] = “gluttonous”

or

pet = {}
pet[ :dog] = “gluttonous”

What do you think is the correct way to cope?

By not doing this:

ARGS = {}

class Args
end

def meth *args
args.each { |arg|
arg.do_sth
ARGS[Args.new] = arg(*args)
}
end

7stud – wrote:

7stud – wrote:

Where is the plugin() method?

ruby is telling you, “Hey guy who names every variable ‘plugin’!
There’s no plugin method defined anywhere.”

Correct, there is no plugin method. Here is some slightly modified code
and the variables it references:

class BackupPlugin
@@IDENTIFIER = “BackupPlugin”

def BackupPlugin.IDENTIFIER
return @@IDENTIFIER
end
def initialize(serve
r, config)
end

def run
end
end

class MessagePlugin
@@IDENTIFIER = “MessagePlugin”
def MessagePlugin.IDENTIFIER
return @@IDENTIFIER
end

def initialize(server, config)
end

def run
end
end

class KickPlugin
@@IDENTIFIER = “KickPlugin”
def KickPlugin.IDENTIFIER
return @@IDENTIFIER
end

def initialize(sever, config)
end

def run
end
end

PLUGINS = [
BackupPlugin,
MessagePlugin,
KickPlugin
]

CONFIG = {
“BackupPlugin” => {
“interval” => (30 * 60),
“directory” => “backups”
}
}

class MyServer

 ---other methods---

def start_plugins
plugins_list = []
PLUGINS.each { |plugin|
if CONFIG.has_key?(plugin.IDENTIFIER)
plugins_list.append(plugin(self, CONFIG[plugin.IDENTIFIER]))
else
plugins_list.append(plugin(self, nil))
end

}

end

end

server = MyServer.popen(COMMAND) #Command is a string shell command to
open a JAR

server.start_plugins

Hi –

On Thu, 17 Sep 2009, Dylan Lukes wrote:

7stud – wrote:

7stud – wrote:

Where is the plugin() method?

ruby is telling you, “Hey guy who names every variable ‘plugin’!
There’s no plugin method defined anywhere.”

Correct, there is no plugin method. Here is some slightly modified code
and the variables it references:

First of all, let’s make Ruby do more of the work :slight_smile: This is
untested, but I think it will give you at least some ideas on how to
neaten up the code. In particular, note that there’s probably no
reason to convert the classes back and forth from strings. (Even if
you need to do that, just use the #name method.)

module Plugin
def initialize(server, config)
end

def run
end
end

class BackupPlugin
include Plugin
end

class MessagePlugin
include Plugin
end

class KickPlugin
include Plugin
end

PLUGINS = [
BackupPlugin,
MessagePlugin,
KickPlugin
]

CONFIG = {
BackupPlugin => {
“interval” => (30 * 60),
“directory” => “backups”
}
}

class MyServer
def start_plugins
PLUGINS.map {|plugin| plugin(self, CONFIG[plugin]) } # !!!
end
end

Of course, the problem is still there. What exactly do you want to do?
plugin is a class. Do you want an instance of it? If so:

plugin.new(self, CONFIG[plugin])

One way or another, you need a method name.

David

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

David
print “#{self} initialized with config:\n\n #{config}\n”
}

#KickPlugin:0x23ed8 initialized with config:

Plugins started…

That’s because CONFIG’s keys are strings. Look at the rewrite I did
earlier; I changed them to class objects. There’s no point doing a
round-trip to string if you really need the class.

David

Bertram S. wrote:

But when using hashes I name the variable without concern:

pets = {}
pets[ :dog] = “gluttonous”

or

pet = {}
pet[ :dog] = “gluttonous”

IMO it depends on whether you are using the hash as an object with
attributes (pet) or as an indexed collection (pets).

Think about what names you would choose inside the || in this code:

pets.each {|type, pet| … }

or

pet.each {|attr, val| … }

David A. Black wrote:

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

David
print “#{self} initialized with config:\n\n #{config}\n”
}

#KickPlugin:0x23ed8 initialized with config:

Plugins started…

That’s because CONFIG’s keys are strings. Look at the rewrite I did
earlier; I changed them to class objects. There’s no point doing a
round-trip to string if you really need the class.

David

I’m not round tripping. I took most of your advice in your rewrite, such
as using name and so on.

I still need to pass the configuration as a parameter to their
initialization though. The only problem is the inherited initialize from
the module Plugin does not seem to be properly getting the config
parameter.

Of course, the problem is still there. What exactly do you want to do?
plugin is a class. Do you want an instance of it? If so:

plugin.new(self, CONFIG[plugin])

One way or another, you need a method name.

David

Okay, I fixed up my code with:

PLUGINS.each {|plugin| plugin = plugin.new(self, CONFIG[plugin])}

The only issue is, on initializing I want it to do the following:

def initialize(server, config)
print “#{self} initialized with config:\n\n #{config}\n”
end

For some reason though, config is coming up blank. I have defined config
for BackupPlugin as a hash including an interval and directory key. The
output shows nothing

CONFIG = {
“BackupPlugin” => {
“interval” => (30 * 60),
“directory” => “backups”
}
}

OUTPUT:

#BackupPlugin:0x23f3c initialized with config:

#MessagePlugin:0x23f28 initialized with config:

#KickPlugin:0x23ed8 initialized with config:

Plugins started…

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

I’m not round tripping. I took most of your advice in your rewrite, such
as using name and so on.

I still need to pass the configuration as a parameter to their
initialization though. The only problem is the inherited initialize from
the module Plugin does not seem to be properly getting the config
parameter.

In your previous message, you had:

 PLUGINS.each {|plugin| plugin = plugin.new(self, CONFIG[plugin])}

and

CONFIG = {
“BackupPlugin” => {
“interval” => (30 * 60),
“directory” => “backups”
}
}

The PLUGINS array contains class objects; the CONFIG hash has keys
that are strings. If I do this:

hash = { SomeClass => “some stuff” }

and then I do:

hash[“SomeClass”]

I’ll get nil, because the hash key is a class object, not a string.

David

David A. Black wrote:

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

I’m not round tripping. I took most of your advice in your rewrite, such
as using name and so on.

I still need to pass the configuration as a parameter to their
initialization though. The only problem is the inherited initialize from
the module Plugin does not seem to be properly getting the config
parameter.

In your previous message, you had:

 PLUGINS.each {|plugin| plugin = plugin.new(self, CONFIG[plugin])}

and

CONFIG = {
“BackupPlugin” => {
“interval” => (30 * 60),
“directory” => “backups”
}
}

The PLUGINS array contains class objects; the CONFIG hash has keys
that are strings. If I do this:

hash = { SomeClass => “some stuff” }

and then I do:

hash[“SomeClass”]

I’ll get nil, because the hash key is a class object, not a string.

David

Ah ok, wonderful. Thank you very much. As a matter of syntax, I was also
wondering how one would do the following:

Print "Config Interval: #{config[“interval”]}

Since the key is a string, it raises some minor issues.

David A. Black wrote:

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

initialization though. The only problem is the inherited initialize from
“interval” => (30 * 60),

Print "Config Interval: #{config[“interval”]}

Since the key is a string, it raises some minor issues.

It should be OK except you’re missing the closing ".

David

That was a type on my part. It appears as such in the program:

Print “Config Interval: #{config[“interval”]}”

Also it does not work fine. It’s interpreted as two distinct strings. I
have tried using /’ or /" but both just spawn more errors…

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

initialization though. The only problem is the inherited initialize from
“interval” => (30 * 60),

Print "Config Interval: #{config[“interval”]}

Since the key is a string, it raises some minor issues.

It should be OK except you’re missing the closing ".

David

Hi –

On Fri, 18 Sep 2009, Dylan Lukes wrote:

Also it does not work fine. It’s interpreted as two distinct strings. I
have tried using /’ or /" but both just spawn more errors…

config = { “interval” => “hello” }
puts “Config Interval: #{config[“interval”]}”

Output: Config Interval: hello

So it’s not a quotation-mark thing. (If you do want to escape quotation
marks, you need to use , not / .)

David