Singleton method on object via define_method?

M

Mauricio Fernández

=> "#<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.
Hm, well ok. But is there an equivalent without evil.rb, i.e. with
"normal" Ruby code? I know that you can access all kinds of internals if
you write an extension, but AFAIK you can't know whether there is a
singleton class or not.

(I assume your last sentence if missing "... without an extension".)

The case above (singletons of singletons) is the only one I can think
of right now where you can always know if the singleton has been created
or not.
 
R

Robert Klemme

Mauricio Fernández said:
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.
(I assume your last sentence if missing "... without an extension".)

You can safely assume this without altering the indended semantics of the
sentence. :)
The case above (singletons of singletons) is the only one I can think
of right now where you can always know if the singleton has been created
or not.

see above.

Kind regards

robert
 
M

Mauricio Fernández

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 :)
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
 
R

Robert Klemme

Mauricio Fernández said:
It is I who doesn't understand what the above is supposed to prove :)

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:
=> 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. :)

Kind regards

robert
 
M

Mauricio Fernández

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.
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...


Hm, but what does this tell me:

=> 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. :)

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 :)
 
M

Michael Granger

--Apple-Mail-2-1043715618
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

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?
--
Michael Granger <[email protected]>
Rubymage, Believer, Architect
The FaerieMUD Consortium <http://www.FaerieMUD.org/>
12383406064495388618631689469409153107.to_s(36).tr('z',' ')

--Apple-Mail-2-1043715618
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (Darwin)

iD8DBQFBSS5Y+zlz4UKpE6QRAs9oAJwNwfBZIsf8clD5BT/WW6/WAmFDDQCfUWbp
6mrTgF4Dyxocs6UA+64SuKQ=
=nHYf
-----END PGP SIGNATURE-----

--Apple-Mail-2-1043715618--
 
R

Robert Klemme

<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 :)

Yah, but that's evil. :)

robert
 
Z

Zach Dennis

Michael said:
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
 
Z

Zach Dennis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top