On Sun, Dec 19, 2010 at 8:01 PM, Abinoam Jr. <abinoam / gmail.com> wrote:
> Try
>
> ruby-1.9.2-head > 5.times { p /thesame/.object_id.to_s + ' ' 
> /thesame/.object_id.to_s}
> "21522620 21522400"
> "21522620 21522400"
> "21522620 21522400"
> "21522620 21522400"
> "21522620 21522400"
> > 5
> ruby-1.9.2-head > 5.times { p 'thesame'.object_id.to_s + ' ' 
> 'thesame'.object_id.to_s}
> "21553480 21553400"
> "21553320 21553240"
> "21553160 21553080"
> "21553000 21552920"
> "21552840 21552760"
> > 5
> ruby-1.9.2-head >
>
> What is the logic behind this?
>

A regular expression literal like /thesameobject/ causes a Regexp
object to be instantiated at PARSE time.

Since regular expressions are immutable (as contrasted with strings or
arrays which also have a literal representation), it really doesn't
mattter if a new regeexp is created each time the expression
containing the literal is evaluated.  The fact that two occurrences of
an apparently 'equal' regular expression generate two different
instances simply reflects the fact that the parser doesn't attempt to
consolidate equal literals.

In the case of a string literal, since strings are mutable, a new
string instance is created each time the expression containing the
string literal is evaluated.


-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale