[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting references

gruby

10/31/2003 4:09:00 PM

How does one get a reference to a previously created object?

Here's the scenario. I'm writing a web application, so there's a
listener object that listens to incoming requests and spins off a
thread for each request. Each thread has a Context object, which holds
things like POST arguments, browser type, etc.

Suppose a request comes in, a thread is spun, and it finds a template
object for that page, fills it with text from a database, and now I
want to run macros. Finding and running macros is ridiculously easy
due to Ruby's regex support. But I want macros to have access to the
Context in which they are running. Is there any way to do this aside
from passing a reference along with each call?
2 Answers

ts

10/31/2003 4:55:00 PM

0

>>>>> "J" == John <gruby@sysarchitects.com> writes:

J> thread for each request. Each thread has a Context object, which holds
J> things like POST arguments, browser type, etc.

Well, if each thread has a Context object then you can store it as a
thread local variable

J> due to Ruby's regex support. But I want macros to have access to the
J> Context in which they are running. Is there any way to do this aside
J> from passing a reference along with each call?

retrieve the thread local variable, something like

svg% cat b.rb
#!/usr/bin/ruby

def retrieve
puts Thread.current['context']
end

Thread.new { Thread.current['context'] = 12; retrieve }
Thread.new { Thread.current['context'] = 24; retrieve }
svg%

svg% b.rb
12
24
svg%


--

Guy Decoux

Robert Klemme

11/3/2003 7:36:00 AM

0


"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:rfc8yn19wjr.fsf@moulon.inra.fr...
> >>>>> "J" == John <gruby@sysarchitects.com> writes:
>
> J> thread for each request. Each thread has a Context object, which
holds
> J> things like POST arguments, browser type, etc.
>
> Well, if each thread has a Context object then you can store it as a
> thread local variable
>
> J> due to Ruby's regex support. But I want macros to have access to the
> J> Context in which they are running. Is there any way to do this aside
> J> from passing a reference along with each call?
>
> retrieve the thread local variable, something like
>
> svg% cat b.rb
> #!/usr/bin/ruby
>
> def retrieve
> puts Thread.current['context']
> end
>
> Thread.new { Thread.current['context'] = 12; retrieve }
> Thread.new { Thread.current['context'] = 24; retrieve }
> svg%
>
> svg% b.rb
> 12
> 24
> svg%

Yeah. Another option is to have a macro interpreter that knows about the
context:

class MacroInterpreter
attr_accessor :context

def expand(macro)
# ...
end
end

Then you have to set it only once and you are not dependend on the thread.
IMHO this is cleaner than passing a hidden method argument / hidden global
(which a thread local effectively is).

Kind regards

robert