Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Getting references

    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?
    John Guest

  2. Similar Questions and Discussions

    1. Web References
      hi, There are 10 machines which run the same webservice. I have 10 webreferences in my client. Only during runtime, will i decide which webservice...
    2. [PHP-DEV] References
      The current support for references is mediocre at best. For instance: class foobar { var $variable; function foobar() { $variable =...
    3. ASP.net using UNC references and IIS 5.0
      I am having trouble getting ANY asp.net application (either .aspx or .asmx) working when IIS is configured to use a "share located on another...
    4. references help
      Hi, I need some desperate help with my references usage in the code below. I'd appreciate the help enormously. Following is the input in the...
    5. references
      Ok lets see I have the following two functions one calls the other Function a (&$myref) { ...something... then calls function b b($myref); ...
  3. #2

    Default Re: Getting references

    >>>>> "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
    ts Guest

  4. #3

    Default Re: Getting references


    "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

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139