On Fri, Apr 18, 2008 at 3:37 PM, Jordan Fowler <[EMAIL PROTECTED]> wrote:
> It's actually a bit more complete than Kevin's in that it correctly inspects
> the contents of the string before trying to get a constant. in other words,
> if the class Mammal::Dog didn't exists, "Mammal::Dog".constantize would
> would raise a NameError exception.
>
> Object.mod_eval will evaluate the given string within the context of the
> Object module (global). The two additional arguments __FILE__ and __LINE__
> are for outputting useful debugging information if there is an error (which
> file and line the error occurred).

Technically mine does this too, though we use a different path to get
there. const_get raises a NameError if the constant doesn't exist
anyway. They spend a regex to figure out if the camel
casing/capitalization exists properly, and then use eval to do the
whole thing at once. Though, I think the internal implementation of
the eval turns into what I've written.

Looks like they're pretty close perf wise, though mine is a little
faster. Eval is usually a hit. Measurement from running
http://p.caboo.se/183556

Odysseus:Desktop kev$ ruby constantize_bench.rb
Constantizing Foo::Bar::Baz
with kevs
  0.090000   0.000000   0.090000 (  0.088796)
  0.090000   0.000000   0.090000 (  0.087270)
  0.080000   0.000000   0.080000 (  0.087258)
  0.090000   0.000000   0.090000 (  0.088146)
  0.090000   0.000000   0.090000 (  0.088272)
with rails
  0.090000   0.000000   0.090000 (  0.092812)
  0.090000   0.000000   0.090000 (  0.092672)
  0.090000   0.000000   0.090000 (  0.091996)
  0.100000   0.000000   0.100000 (  0.092787)
  0.090000   0.000000   0.090000 (  0.092451)
Constantizing Foo::Bar::Baz
with kevs
  0.080000   0.000000   0.080000 (  0.086750)
  0.090000   0.010000   0.100000 (  0.086792)
  0.090000   0.000000   0.090000 (  0.088166)
  0.080000   0.000000   0.080000 (  0.087048)
  0.090000   0.000000   0.090000 (  0.088431)
with rails
  0.100000   0.000000   0.100000 (  0.096574)
  0.090000   0.000000   0.090000 (  0.091886)
  0.090000   0.000000   0.090000 (  0.092633)
  0.090000   0.000000   0.090000 (  0.091968)
  0.100000   0.000000   0.100000 (  0.092772)
Constantizing Foo::Bar::Buz
with kevs
  0.080000   0.000000   0.080000 (  0.086349)
  0.090000   0.000000   0.090000 (  0.087658)
  0.090000   0.000000   0.090000 (  0.086615)
  0.080000   0.000000   0.080000 (  0.086415)
  0.090000   0.000000   0.090000 (  0.086572)
with rails
  0.090000   0.000000   0.090000 (  0.091927)
  0.090000   0.000000   0.090000 (  0.091546)
  0.090000   0.000000   0.090000 (  0.091912)
  0.100000   0.000000   0.100000 (  0.092360)
  0.090000   0.000000   0.090000 (  0.091962)


-- 
Kevin Clark
http://glu.ttono.us

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to