From: Matthew Kerwin Date: 2013-04-11T20:26:38+09:00 Subject: Re: confusion with Struct class --bcaec54eea70274e7004da140f96 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Apr 11, 2013 8:19 PM, "Love U Ruby" wrote: > > "Jes=FAs Gabriel y Gal=E1n" wrote in post > #1105232: > > On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby > > wrote: > >> > > If you read the doc you referenced closely: > > > ***the name of this struct > > will appear as a constant in class Struct***, > > Yes,I am confused how a full class be a constant. We can create a > variable as a constant, say "NAME". class and module names are also > constant. But full class body is constant - I just newly step into this > topic. Remember that all ruby values are objects, even classes. # classes class Foo; end Bar =3D class.new $baz =3D Foo qux =3D Bar # instance objects Foo.new Bar.new $baz.new qux.new It's all, mostly, the same. And a 'constant' is just a variable that gets a special warning when assgined to, but aside from that it's not particularly magical. MyConst =3D [] # all good: MyConst << 1 MyConst.unshift 7 MyConst.sort! MyConst.delete_if { |e| e<3 } def MyConst.foo; end # not good: MyConst =3D [7] You can modify the _object_, but you can't modify the reference. --bcaec54eea70274e7004da140f96 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Apr 11, 2013 8:19 PM, "Love U Ruby" <lists@ruby-forum.com> wrote:
>
> "Jes=FAs Gabriel y Gal=E1n" <jgabrielygalan@gmail.com> wrote in post
> #1105232:
> > On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby <lists@ruby-forum.com>
> > wrote:
> >>
> > If you read the doc you referenced closely:
> >
> =A0***the name of this struct
> > will appear as a constant in class Struct***,
>
> Yes,I am confused how a full class be a constant. We can create a
> variable as a constant, say "NAME". class and module names a= re also
> constant. But full class body is constant - I just newly step into thi= s
> topic.

Remember that all ruby values are objects, even classes.

=A0=A0=A0 # classes
=A0=A0=A0 class Foo; end
=A0=A0=A0 Bar =3D class.new
=A0=A0=A0 $baz =3D Foo
=A0=A0=A0 qux =3D Bar

=A0=A0=A0 # instance objects
=A0=A0=A0 Foo.new
=A0=A0=A0 Bar.new
=A0=A0=A0 $baz.new
=A0=A0=A0 qux.new

It's all, mostly, the same.

And a 'constant' is just a variable that gets a special warning = when assgined to, but aside from that it's not particularly magical.

=A0=A0=A0 MyConst =3D []
=A0=A0=A0 # all good:
=A0=A0=A0 MyConst << 1
=A0=A0=A0 MyConst.unshift 7
=A0=A0=A0 MyConst.sort!
=A0=A0=A0 MyConst.delete_if { |e| e<3 }
=A0=A0=A0 def MyConst.foo; end
=A0=A0=A0 # not good:
=A0=A0=A0 MyConst =3D [7]

You can modify the _object_, but you can't modify the reference.

--bcaec54eea70274e7004da140f96--