Faking constants with methods

Hi.

I was wondering why this wouldn’t work:

class Bar
end

class Foo
def initialize
# doesn’t call the method, but gives me the class instead.
p Bar.inspect
end

def Bar
  "whatever"
end

end

I know this is bad style anyway, but would be kind of convenient in
rails controllers:

def ProfileField
ProfileField.for_user(current_user) # for_user being a scope
end

somewhere else in controller:

ProfileField.find(:all)

Anyone got an idea how to “fake” constants that way?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 15.11.2010 10:21, schrieb niklas | brueckenschlaeger:

Anyone got an idea how to “fake” constants that way?

Maybe with const_missing?
http://www.ruby-doc.org/ruby-1.9/classes/Module.html#M000928

class Foo

def self.const_missing(sym)
“You wanted to know #{sym}, but it doesn’t exist!”
end

end

puts Foo::ABC #=> You wanted to know ABC, but it doesn’t exist!
puts Foo::Bar #=> You wanted to know Bar, but it doesn’t exist!

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJM4QLWAAoJEGrS0YjAWTKVW6sH/R6bz79ile/ILx5ATj5wAGNl
TiqdbB0MC/Vhjchuq5QW6Gn6+Tk4JBFREvyA4k9h/UrFcW+rXz8XmZr/jly+xK1z
wvaL+ifk5LnwV2aFzI70cWip2lcxU/yP92KwW6/FSaT3OAolmOekB0lKu+l73WDr
BJDT9SCcfD5jBoOPR/BFik91STwue1B/9//8+M8U7RaU7PXbtV/Lxd6jW37KvGb2
O9gj908pBw4zF3VjBmPGTYekIfpc/q6E02WretcmaqTAk5XzNmFkNf6p8dkNq5u2
nVQAq+C6BmoQfpOIDpuHtgSwbwZoJ3wYPlt9NhLUdQTuyWj/tUEbU/2SIDw0fCw=
=pWPT
-----END PGP SIGNATURE-----

Thank you, but that would only work if the constant isn’t defined. Maybe
I wasn’t being clear: I want to override a constant within an object
with a method.

class Bar
end

class Foo
def Bar
end

def some_method
  Bar.something # this resolves to constant Bar, not method Bar
end

end

Well, if I were to change the code that uses the constant, I would
change it to a less ambiguous method call. My point is that I want to
keep the rest of the code as is, hiding the fact that it’s something
else.
But I guess I’ll just leave it and stop investigating on how to write
surprisingly bad code…

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 15.11.2010 11:02, schrieb niklas | brueckenschlaeger:

def some_method
  Bar.something # this resolves to constant Bar, not method Bar
end

end

That should be possible with #send:

class Bar
end

class Foo
def Bar
end

def some_method
self.send(:Bar).something
end
end

Not nice to read, but works. But I’m not sure if that meets your
original intention of a convenient method to do it…

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJM4QmhAAoJEGrS0YjAWTKV12cIAKDMZo7lLCz4QXLolqZ6abdS
BP5yKOuB68IYXBQ6WLTB1EbF/9FNcIIZZ1qGO9CMiThsuX1nPL7uP8Q4sr88pM+Z
rN3dDbcGZ3ZUt+mQHHaTMSW+oWX+rf/DQ5hs/Faaq3QkcPdgupogyaKPVsuWNmto
kktVFoXBxuuQ+I1HNUUaTbvQ2tuClD3rQflUsUJO5d5utLi22KLna6me5dEJuSci
kteL6lpaWqmHp3pCyVR3GODb6fON9IBi2jDHKLjCscIk/DTZWJGxMs7kIeMvPhPY
vxvNSqCMAOtdDKtzg+uKM9O7jXKdZHzC6IqnJoIprAW3HCB4XBZ5Cr/0vt4CVbU=
=ZMgq
-----END PGP SIGNATURE-----