Singleton method on object via define_method?

I've got a dummy site up and working with Arrow, but I have a question....In the httpd.conf file you specify something like:

RubyAddPath /usr/local/lib/ruby/site_ruby/1.8
RubyRequire arrow
<Location /camphq>
   SetHandler ruby-object
   RubyHandler "Arrow::Dispatcher::instance( '/var/www/camphq/config/camphq.cfg' )"
</Location>

Where /camphq is how the url of how you want to access your site. IE: http://mysite.com/camphq . However in my Arrow config file you have something like:

applets:
  defaultApplet: "/index" #Note this line
  missingApplet: "/missing"
  errorApplet: "/error"
  pattern: "*.rb"
  path:
    - "/var/www/camphq/applets"
  pollInterval: 5
  config: {}
  layout:
    "/index": Index # Note this line to

You would think that if you went to http://mysite.com/camphq/index it would run the defaultApplet "/index" (which actually tells the layout "/index" to run ). But it doesn't! Instead you have to say:

applets:
  defaultApplet: "/camphq/index" #Note this line
  missingApplet: "/missing"
  errorApplet: "/error"
  pattern: "*.rb"
  path:
    - "/var/www/camphq/applets"
  pollInterval: 5
  config: {}
  layout:
    "/camphq/index": Index # Note this line to

in order for the url to work (http://mysite.com/camphq/index ). Is there a way around this. I would like to assume that the URL root specified in the httpd.conf file was the root of my Arrow-based "Portal" (can i call it that?) Thanks!!

Zach

In article <44679.129.94.6.30.1095124491.squirrel@webmail.imagineis.com>,

Phil wrote:

module Foo1
   def foo; "foo1"; end
end

module Foo2
   def foo; "foo2"; end
end

class X
end

x = X.new

x.foo # error

x.extend Foo1
x.foo # "foo1"

x.extend Foo2
x.foo # "foo2"

x.unextend Foo2
x.foo # "foo1"

Yes, you're right. This would be better behavior. Any idea how
something like that could be implemented?

Yep: in the Ruby core! :slight_smile:

Yeah, I'm afraid you're probably right (at least to do it 'nicely')

Only pure-Ruby solution I can think of is to remember the modules that
have been mixed in, and selectively reapply earlier ones when 'unextend'
is called. Pretty fugly.

Perhaps, but it's probably possible. Is there an 'extended' callback? if not
'extend' could be redefined.

Phil

···

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:

It is I who doesn't understand what the above is supposed to prove :slight_smile:
Before creating the singleton class of the singleton of o's singleton
class, to_s returned "foo", afterwards it returns "#<Class:foo>"...
And anyway, nobody forces us to use to_s

batsman@tux-chan:/tmp$ cat dfg.rb
class Class
  # only works for singletons of singletons, will always return
  # true otherwise
    def has_singleton?
        m = "__magic_#{Time.new.to_i}_#{rand(100000)}"
        class_eval do
            remove_method(m) rescue nil
            define_method(m) { }
            begin
                send m
            rescue NameError
                return true
            ensure
                remove_method m
            end
        end
        false
    end
end

o = Object.new
klass = class << o; class << self; self end end
p klass.has_singleton?
class << klass; end
p klass.has_singleton?

batsman@tux-chan:/tmp$ ruby dfg.rb
false
true

···

On Wed, Sep 15, 2004 at 09:49:50PM +0900, Robert Klemme wrote:

> > >> o = Object.new
> > => #<Object:0x10184d40>
> > >> class << o; class << self; def to_s ; "foo" end end end
> > => nil
> > >> class << o; class << self;to_s end end
> > => "foo"
> > >> class << o; class << self; class << self; end end end
> > => nil
> > >> class << o; class << self; to_s end end
> > => "#<Class:foo>"
>
> It's using Module#to_s because the klass of the singleton class is
> no longer itself... Module#to_s ends up calling
> class << o; self.to_s end
> which you defined before.

Well yes, but the fact remains that there is a singleton we don't know of,
do we? Probably I don't see clearly your point so I'd appreciate it if
you'd elaborate it.

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Phil wrote:

Only pure-Ruby solution I can think of is to remember the modules that
have been mixed in, and selectively reapply earlier ones when
'unextend' is called. Pretty fugly.

Perhaps, but it's probably possible. Is there an 'extended' callback?
if not 'extend' could be redefined.

The callback you're after is Module#append_features, from memory. On
first thought, I doubt that's useful, because it's from each individual
module's POV.

If I were implementing this, I'd create new methods rather than modify
#entend, at least at first. Something like:

  Object#wrap(module)
  Object#unwrap(module)

Perhaps a traits lit. review would turn up some agreeable terminology.

Cheers,
Gavin

"Mauricio Fernández" <batsman.geo@yahoo.com> schrieb im Newsbeitrag news:20040915170634.GA25725@student.ei.uni-stuttgart.de...

> > >> o = Object.new
> > => #<Object:0x10184d40>
> > >> class << o; class << self; def to_s ; "foo" end end end
> > => nil
> > >> class << o; class << self;to_s end end
> > => "foo"
> > >> class << o; class << self; class << self; end end end
> > => nil
> > >> class << o; class << self; to_s end end
> > => "#<Class:foo>"
>
> It's using Module#to_s because the klass of the singleton class is
> no longer itself... Module#to_s ends up calling
> class << o; self.to_s end
> which you defined before.

Well yes, but the fact remains that there is a singleton we don't know of,
do we? Probably I don't see clearly your point so I'd appreciate it if
you'd elaborate it.

It is I who doesn't understand what the above is supposed to prove :slight_smile:

Utter confusion on all sides. :-)))

My understanding: someone (I?) made a remark about the fact (well, assumed fact) that you can't test whether an instance has a singleton class. You can access it, but you will not know whether it was created for this access or whether it existed before. You then presented the example with a NameError due to undefined method foo. I thought, you use the NameError for detecting the singleton class, hence I used to_s which I know is always defined and thus would not trigger the exception. The idea being to disprove the test. Maybe I got something wrong here...

Before creating the singleton class of the singleton of o's singleton
class, to_s returned "foo", afterwards it returns "#<Class:foo>"...
And anyway, nobody forces us to use to_s

batsman@tux-chan:/tmp$ cat dfg.rb
class Class
# only works for singletons of singletons, will always return
# true otherwise
   def has_singleton?
       m = "__magic_#{Time.new.to_i}_#{rand(100000)}"
       class_eval do
           remove_method(m) rescue nil
           define_method(m) { }
           begin
               send m
           rescue NameError
               return true
           ensure
               remove_method m
           end
       end
       false
   end
end

I'm not sure whether I fully understand what the test should do. But it's late and I'll better go to bed now. A brain with some sleep thinks better...

o = Object.new
klass = class << o; class << self; self end end
p klass.has_singleton?
class << klass; end
p klass.has_singleton?

batsman@tux-chan:/tmp$ ruby dfg.rb
false
true

Hm, but what does this tell me:

o = Object.new

=> #<Object:0x10194858>

klass = class << o; self end

=> #<Class:#<Object:0x10194858>>

klass.has_singleton?

=> true

The answer is definitely wrong, because the singleton class of o does not have a singleton klass. Hm...

For the moment I'll stick with my statement that it's generally not possible to determine whether an instance has a singleton or not other than by using some kind of extension. :slight_smile:

Kind regards

    robert

···

On Wed, Sep 15, 2004 at 09:49:50PM +0900, Robert Klemme wrote:

I've got a dummy site up and working with Arrow, but I have a question....In the httpd.conf file you specify something like:
[...]
You would think that if you went to http://mysite.com/camphq/index it would run the defaultApplet "/index" (which actually tells the layout "/index" to run ). But it doesn't! Instead you have to say:

Yes, this is how it's supposed to work.

applets:
defaultApplet: "/camphq/index" #Note this line
missingApplet: "/missing"
errorApplet: "/error"
pattern: "*.rb"
path:
   - "/var/www/camphq/applets"
pollInterval: 5
config: {}
layout:
   "/camphq/index": Index # Note this line to

in order for the url to work (http://mysite.com/camphq/index ). Is there a way around this. I would like to assume that the URL root specified in the httpd.conf file was the root of my Arrow-based "Portal" (can i call it that?) Thanks!!

I'm at a loss to explain what's going on, as I can't reproduce this. When I map something in the layout as '/camphq/index', I have to go to /camphq/camphq/index to get it to run. Would you mind bumping your LogLevel in httpd.conf and logLevel in your camphq.conf up to 'debug' and mailing me the output in your error log for one request to /camphq/index?

PGP.sig (186 Bytes)

···

On Sep 13, 2004, at 6:29 PM, Zach Dennis wrote:
--
Michael Granger <ged@FaerieMUD.org>
Rubymage, Believer, Architect
The FaerieMUD Consortium <http://www.FaerieMUD.org/&gt;
12383406064495388618631689469409153107.to_s(36).tr('z',' ')

>>Well yes, but the fact remains that there is a singleton we don't know
>>of,
>>do we? Probably I don't see clearly your point so I'd appreciate it if
>>you'd elaborate it.
>
>It is I who doesn't understand what the above is supposed to prove :slight_smile:

Utter confusion on all sides. :-)))

My understanding: someone (I?) made a remark about the fact (well, assumed
fact) that you can't test whether an instance has a singleton class. You
can access it, but you will not know whether it was created for this access
or whether it existed before. You then presented the example with a
NameError due to undefined method foo.

It's more than a NameError, it's an anomaly in Ruby's object model that
I can detect as a *successful* call that should fail. You get the
NameError when that anomalous situation is no longer there (pushed one
singleton away, so to speak).

I thought, you use the NameError
for detecting the singleton class, hence I used to_s which I know is always

But I wouldn't use #to_s for the test *g*

defined and thus would not trigger the exception. The idea being to
disprove the test. Maybe I got something wrong here...

That could be detected and a new (random) name would be used.

>batsman@tux-chan:/tmp$ cat dfg.rb
>class Class
># only works for singletons of singletons, will always return
># true otherwise
> def has_singleton?
> m = "__magic_#{Time.new.to_i}_#{rand(100000)}"
> class_eval do
> remove_method(m) rescue nil
> define_method(m) { }
> begin
> send m
> rescue NameError
> return true
> ensure
> remove_method m
> end
> end
> false
> end
>end

I'm not sure whether I fully understand what the test should do. But it's
late and I'll better go to bed now. A brain with some sleep thinks
better...

>o = Object.new
>klass = class << o; class << self; self end end
>p klass.has_singleton?
>class << klass; end
>p klass.has_singleton?
>
>batsman@tux-chan:/tmp$ ruby dfg.rb
>false
>true

Hm, but what does this tell me:

>>o = Object.new
=> #<Object:0x10194858>
>>klass = class << o; self end
=> #<Class:#<Object:0x10194858>>
>>klass.has_singleton?
=> true

The answer is definitely wrong, because the singleton class of o does not
have a singleton klass. Hm...

Hence the warning in my code:
  # only works for singletons of singletons, will always return
  # true otherwise
that is, it only says the truth when called on singletons of singletons.
In the snippet above, you called it on a singleton of a non-singleton.

For the moment I'll stick with my statement that it's generally not
possible to determine whether an instance has a singleton or not other than
by using some kind of extension. :slight_smile:

We're both right, it is generally impossible, and "there's at least one
case where you can know if the singleton class has been created or not
(singletons of singletons)".

(And you can always know if you're using evil.rb, of course :slight_smile:

···

On Thu, Sep 16, 2004 at 07:39:49AM +0900, Robert Klemme wrote:

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Michael Granger wrote:

I've got a dummy site up and working with Arrow, but I have a question....In the httpd.conf file you specify something like:
[...]
You would think that if you went to http://mysite.com/camphq/index it would run the defaultApplet "/index" (which actually tells the layout "/index" to run ). But it doesn't! Instead you have to say:

Yes, this is how it's supposed to work.

applets:
defaultApplet: "/camphq/index" #Note this line
missingApplet: "/missing"
errorApplet: "/error"
pattern: "*.rb"
path:
   - "/var/www/camphq/applets"
pollInterval: 5
config: {}
layout:
   "/camphq/index": Index # Note this line to

in order for the url to work (http://mysite.com/camphq/index ). Is there a way around this. I would like to assume that the URL root specified in the httpd.conf file was the root of my Arrow-based "Portal" (can i call it that?) Thanks!!

I'm at a loss to explain what's going on, as I can't reproduce this. When I map something in the layout as '/camphq/index', I have to go to /camphq/camphq/index to get it to run. Would you mind bumping your LogLevel in httpd.conf and logLevel in your camphq.conf up to 'debug' and mailing me the output in your error log for one request to /camphq/index?

Here is my error.log output:

[Sun Sep 19 16:23:54 2004] [error] [client 10.0.1.61] File does not exist: /var/www/camphq.com/favicon.ico
[Sun Sep 19 16:23:58 2004] [notice] (global) : Arrow config file is "/var/www/camphq/config/camphq.cfg"
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Configuring a dispatcher for 'camphq.com': child server 13833
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Monitor skipped by configuration
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Setting global log level to :debug
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Configuring the Session class with #<Arrow::Config:0x40721e24 @loader=#<Arrow::Config::YamlLoader:0x40729bb0>, @name="/var/www/camphq/config/camphq.cfg", @createTime=Sun Sep 19 16:23:58 UTC 2004, @struct=#<Arrow::Config::ConfigStruct:0x4071fae8 @modified=false, @hash={:templates=>{:loader=>"Arrow::Template", :path=>["/var/www/camphq/templates"], :cacheConfig=>{:maxSize=>2621440, :expiration=>36, :maxNum=>20, :maxObjSize=>131072}, :cache=>false}, :startMonitor=>false, :session=>{:storeType=>"file:/tmp", :expires=>"+48h", :idName=>"arrow-session", :rewriteUrls=>true, :idType=>"md5:.", :lockType=>"recommended"}, :applets=>{:missingApplet=>"/missing", :path=>["/var/www/camphq/applets"], :pollInterval=>5, :pattern=>"*.rb", :errorApplet=>"/error", :defaultApplet=>"/index", :config=>{}, :layout=>{:"/index"=>"Index", :"/camphq/protected/index"=>"Index", :"/camphq/protected"=>"ProtectedDelegator"}}, :logLevel=>"debug", :unittester=>{:dbenv=>"/www/data/applettests"}, :templateLogLevel=>"debug"}>>
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Creating request broker
[Sun Sep 19 16:23:58 2004] [error] Arrow::Broker : Configured MissingApplet handler (/missing) doesn't exist
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Broker : Using builtin missing-applet handler.
[Sun Sep 19 16:23:58 2004] [error] Arrow::Broker : Applet returned false value. Setting status to DECLINED
[Sun Sep 19 16:23:58 2004] [notice] Arrow::Dispatcher : Transaction has non-OK status: -1
[Sun Sep 19 16:23:58 2004] [error] [client 10.0.1.61] File does not exist: /var/www/camphq.com/camphq/index
[Sun Sep 19 16:23:58 2004] [error] [client 10.0.1.61] File does not exist: /var/www/camphq.com/favicon.ico

Thanks,

Zach

···

On Sep 13, 2004, at 6:29 PM, Zach Dennis wrote:

"Mauricio Fernández" <batsman.geo@yahoo.com> schrieb im Newsbeitrag
news:20040915233138.GA2327@student.ei.uni-stuttgart.de...

<snip/>

Thanks for the wrap up!

We're both right, it is generally impossible, and "there's at least one
case where you can know if the singleton class has been created or not
(singletons of singletons)".

What a nice solution. :-))

(And you can always know if you're using evil.rb, of course :slight_smile:

Yah, but that's evil. :slight_smile:

    robert

Michael,

I found my problem. My Apache httpd.conf file was pointing to "/var/www/camphq.com" as the directory root. That directory did not exist. but "/var/www/camphq" did.

When I supplied a full URL: "http://www.camphq.com/index" it couldn't find index because it was looking for index in the /var/www/camphq.com/applets directory. When I supplied the "camphq" post-fix on the URL then Arrow was able to correctly handle it because I told it to specifically go to /camphq.

I made a symlink from /var/www/camphq.com to /var/www/camphq and switched my configuration file for Arrow, and it is working like a charm.

Sorry for the confusion.

Zach