I like working in the style of Java (one class per file) for some projects
and I’ve emulated it for an email/webagent app. What I’m wondering is if
there is a better or builtin way to do this. Here is my code:
def loadAgents(path)
Dir[path + “*Agent.rb”].each { |agent|
agent = File.basename(agent)
if agent =~ /^((\w+)Agent).rb/
require $1
$agentFactory[$2.downcase] = $1
end
}
end
I initialize ‘$agentFactory’ to ‘{}’ and add ‘lib/’ to $: before calling
loadAgents(). Then I can ‘object = agentFactory[request].new()’. Also I
can add agents just by writing the class in a new agent file in lib and
restarting.
I guess another question would be how can I reload ‘lib/’ without
restarting? Does ‘load’ replace class definitions (it seems require is a
’one time thing’)?
Thanks! (I hope that made sense – it does in my mind, but that’s where I
live – not you 
Greg B.
Greg Brondo wrote:
I like working in the style of Java (one class per file) for some projects
and I’ve emulated it for an email/webagent app. What I’m wondering is if
there is a better or builtin way to do this. Here is my code:
def loadAgents(path)
Dir[path + “*Agent.rb”].each { |agent|
agent = File.basename(agent)
if agent =~ /^((\w+)Agent).rb/
require $1
$agentFactory[$2.downcase] = $1
end
}
end
I initialize ‘$agentFactory’ to ‘{}’ and add ‘lib/’ to $: before calling
loadAgents(). Then I can ‘object = agentFactory[request].new()’. Also I
can add agents just by writing the class in a new agent file in lib and
restarting.
I guess another question would be how can I reload ‘lib/’ without
restarting? Does ‘load’ replace class definitions (it seems require is a
‘one time thing’)?
Thanks! (I hope that made sense – it does in my mind, but that’s where I
live – not you 
Greg B.
Um, ok, it worked in IRB but not in my script. The hash if referencing a
string instead of the agent class…Hmmm…
I like working in the style of Java (one class per file) for some projects
and I’ve emulated it for an email/webagent app. What I’m wondering is if
there is a better or builtin way to do this. Here is my code:
def loadAgents(path)
Dir[path + “*Agent.rb”].each { |agent|
agent = File.basename(agent)
if agent =~ /^((\w+)Agent).rb/
require $1
$agentFactory[$2.downcase] = $1
end
}
end
I suppose this is reasonable, even if I’d simplify it using an agent/
directory, and I would it like this:
def loadAgents
Dir[‘Agents/*.rb’].each do |agent|
load agent
end
def agent_create(name)
Object::const_get(name).new
end
or at least I’d use a constant
AgentFactory instead of an ugly dollar-var.
I initialize ‘$agentFactory’ to ‘{}’ and add ‘lib/’ to $: before calling
loadAgents(). Then I can ‘object = agentFactory[request].new()’. Also I
can add agents just by writing the class in a new agent file in lib and
restarting.
I guess another question would be how can I reload ‘lib/’ without
restarting? Does ‘load’ replace class definitions (it seems require is a
‘one time thing’)?
yes, require is a one time thing, and load works as expected:
load ‘agen.rb’
=> true
Agent.new
=> #<Agent:0x27f6bc0 @a=1>
class Agent
def initialize
@b=2
end
end
=> nil
Agent.new
=> #<Agent:0x27dbe60 @b=2>
load ‘agen.rb’
=> true
Agent.new
=> #<Agent:0x27f68d8 @a=1>
···
il Wed, 31 Mar 2004 17:24:54 GMT, Greg Brondo greg@brondo.com ha scritto::
Greg Brondo wrote:
I like working in the style of Java (one class per file) for some projects
and I’ve emulated it for an email/webagent app. What I’m wondering is if
there is a better or builtin way to do this. Here is my code:
def loadAgents(path)
Dir[path + “*Agent.rb”].each { |agent|
Maybe do ?
Dir[File.join(path, “*Agent.rb”]
agent = File.basename(agent)
if agent =~ /^((\w+)Agent)\.rb/
require $1
$agentFactory[$2.downcase] = $1
end
}
end
watch out about using the global variables, while doing something in the
meantime (require). Who knows maybe the required file will modify $2 ?
Instead I suggest doing (untested)
match = agent.match /^((\w+)Agent).rb/
if match
require match[1]
$agentFactory[match[2].downcase] = match[1]
end
I initialize ‘$agentFactory’ to ‘{}’ and add ‘lib/’ to $: before calling
loadAgents(). Then I can ‘object = agentFactory[request].new()’. Also I
can add agents just by writing the class in a new agent file in lib and
restarting.
I guess another question would be how can I reload ‘lib/’ without
restarting? Does ‘load’ replace class definitions (it seems require is a
‘one time thing’)?
Yes, you are correct about require.
There is nothing special about load, it simply loads the file and
evaluates the content. However load doesn’t replace classes.
Ruby has a concept named class reopening. The first time you evaluate the
class its being created, the succeding times you evaluate the class then
you will reopen the class.
···
On Wed, 31 Mar 2004 18:31:14 +0000, Greg Brondo wrote:
–
Simon Strandgaard
gabriele renzi wrote:
il Wed, 31 Mar 2004 17:24:54 GMT, Greg Brondo greg@brondo.com ha
scritto::
I like working in the style of Java (one class per file) for some projects
and I’ve emulated it for an email/webagent app. What I’m wondering is if
there is a better or builtin way to do this. Here is my code:
def loadAgents(path)
Dir[path + “*Agent.rb”].each { |agent|
agent = File.basename(agent)
if agent =~ /^((\w+)Agent).rb/
require $1
$agentFactory[$2.downcase] = $1
end
}
end
I suppose this is reasonable, even if I’d simplify it using an agent/
directory, and I would it like this:
def loadAgents
Dir[‘Agents/*.rb’].each do |agent|
load agent
end
def agent_create(name)
Object::const_get(name).new
end
or at least I’d use a constant
AgentFactory instead of an ugly dollar-var.
I initialize ‘$agentFactory’ to ‘{}’ and add ‘lib/’ to $: before calling
loadAgents(). Then I can ‘object = agentFactory[request].new()’. Also I
can add agents just by writing the class in a new agent file in lib and
restarting.
I guess another question would be how can I reload ‘lib/’ without
restarting? Does ‘load’ replace class definitions (it seems require is a
‘one time thing’)?
yes, require is a one time thing, and load works as expected:
load ‘agen.rb’
=> true
Agent.new
=> #<Agent:0x27f6bc0 @a=1>
class Agent
def initialize
@b=2
end
end
=> nil
Agent.new
=> #<Agent:0x27dbe60 @b=2>
load ‘agen.rb’
=> true
Agent.new
=> #<Agent:0x27f68d8 @a=1>
Woo hoo! thanks much. I could not find the info about const_get(name).
Works like a champ!