# Confusion with Struct class

**URL:** <https://rubytalk.org/t/confusion-with-struct-class/67690>\
**Category:** ruby-talk\
**Created:** [11 April 2013 08:45 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690 "2013-04-11T08:45:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 08:45 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/1 "2013-04-11T08:45:27Z")

</div>

I went to there - [http://www.ruby-doc.org/core-2.0/Struct.html](http://www.ruby-doc.org/core-2.0/Struct.html) but the  
below mentioned not cleared.

Customer = Struct.new(:name, :address)  
p Customer.class #=\>class

Struct.new("Customer", :name, :address)  
p Customer.class #`\<main\>': uninitialized constant Customer (NameError)

Can anyone help me what's the difference between these two construct?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [11 April 2013 09:05 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/2 "2013-04-11T09:05:09Z")

</div>

If you read the doc you referenced closely:

"Creates a new class, named by aString, containing accessor methods  
for the given symbols. If the name aString is omitted, an anonymous  
structure class will be created. Otherwise, \*\*\*the name of this struct  
will appear as a constant in class Struct\*\*\*, so it must be unique for  
all Structs in the system and should start with a capital letter.  
Assigning a structure class to a constant effectively gives the class  
the name of the constant."

Check the part between \*\*\*

1.9.2p290 :001 \> Struct.new("Customer", :name, :address)  
=\> Struct::Customer  
1.9.2p290 :002 \> p Struct::Customer  
Struct::Customer  
=\> Struct::Customer  
1.9.2p290 :003 \> p Struct::Customer.class  
Class  
=\> Class

Jesus.

> **···**
>
> On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > I went to there - [Class: Struct (Ruby 2.0.0)](http://www.ruby-doc.org/core-2.0/Struct.html) but the  
> > below mentioned not cleared.
> > 
> > Customer = Struct.new(:name, :address)  
> > p Customer.class #=\>class
> > 
> > Struct.new("Customer", :name, :address)  
> > p Customer.class #`\<main\>': uninitialized constant Customer (NameError)
> > 
> > Can anyone help me what's the difference between these two construct?

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 13:59 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/3 "2013-04-11T13:59:58Z")

</div>

Thanks @Matthew and @Jesús.

Customer = Struct.new(:name, :address)

&nbsp;&nbsp;and

Struct.new("Customer", :name, :address)

Is both the declaration will work same way? Means any more differences  
with the above two approaches?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 14:27 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/4 "2013-04-11T14:27:25Z")

</div>

Why does every time the has value getting changed,while the instance  
variables values are not getting changed?

Customer = Struct.new(:name, :address, :zip)  
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)

p joe.hash  
p joe.object\_id

#-93880937  
#8577648

p joe.hash  
p joe.object\_id

#275982710  
#8577648

p joe.hash  
p joe.object\_id

#767998854  
#8577648

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 17:38 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/5 "2013-04-11T17:38:17Z")

</div>

Here is another confusion:

I tried the below code:

> **···**
>
> ===============================  
> Customer = Struct.new(:name, :address) do |x|  
> &nbsp;&nbsp;def x.greeting  
> &nbsp;&nbsp;&nbsp;&nbsp;"Hello #{name}!"  
> &nbsp;&nbsp;end  
> end
> 
> # p Customer.greeting #=\> "Hello Customer!"
> 
> Now my question is - How does instance variable has been assigned to  
> `Customer`?
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 10:19 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/6 "2013-04-11T10:19:11Z")

</div>

"Jesús Gabriel y Galán" \<jgabrielygalan@gmail.com\> wrote in post  
#1105232:

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

> **···**
>
> > On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby \<lists@ruby-forum.com\> \> wrote:
> 
> > 1.9.2p290 :001 \> Struct.new("Customer", :name, :address)  
> > =\> Struct::Customer  
> > 1.9.2p290 :002 \> p Struct::Customer  
> > Struct::Customer  
> > =\> Struct::Customer  
> > 1.9.2p290 :003 \> p Struct::Customer.class  
> > Class  
> > =\> Class
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://rubytalk.org/u/Julian_Leviston)\
**Post date:** [11 April 2013 14:12 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/7 "2013-04-11T14:12:58Z")

</div>

> Thanks @Matthew and @Jesús.
> 
> Customer = Struct.new(:name, :address)
> 
> and
> 
> Struct.new("Customer", :name, :address)
> 
> Is both the declaration will work same way? Means any more differences  
> with the above two approaches?
> 
> Thanks
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

Customer = Struct.new(:name, :address)

You'll have to use Customer.new to create new customers, but in:

Struct.new("Customer", :name, :address)

It'll register the Customer class inside the Struct class, so you'll have to refer to it like this:

Struct::Customer.new

Struct is a class that GENERATES other classes for you. The syntax where you pass in the name of the class registers the new class inside the struct class... the other syntax doesn't, it just registers it against whatever you assign it to.

Point in case:

> > x = Struct.new(:name, :address)

=\> #\<Class:0x007fbcea91ed68\>

> > x.new

=\> #\<struct name=nil, address=nil\>

Julian

> **···**
>
> On 11/04/2013, at 11:59 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 April 2013 14:44 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/8 "2013-04-11T14:44:32Z")

</div>

What the heck are you doing?

irb(main):004:0\> Customer = Struct.new(:name, :address, :zip)  
=\> Customer  
irb(main):005:0\> joe = Customer.new("Joe Smith", "123 Maple, Anytown NC",  
12345)  
=\> #\<struct Customer name="Joe Smith", address="123 Maple, Anytown NC",  
zip=12345\>  
irb(main):006:0\> 5.times { puts joe.hash }  
1060632465  
1060632465  
1060632465  
1060632465  
1060632465  
=\> 5  
irb(main):007:0\> 5.times { puts Customer.new("Joe Smith", "123 Maple,  
Anytown NC", 12345).hash }  
1060632465  
1060632465  
1060632465  
1060632465  
1060632465  
=\> 5

robert

> **···**
>
> On Thu, Apr 11, 2013 at 4:27 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > Why does every time the has value getting changed,while the instance  
> > variables values are not getting changed?
> > 
> > Customer = Struct.new(:name, :address, :zip)  
> > joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
> > 
> > p joe.hash  
> > p joe.object\_id
> > 
> > #-93880937  
> > #8577648
> > 
> > p joe.hash  
> > p joe.object\_id
> > 
> > #275982710  
> > #8577648
> > 
> > p joe.hash  
> > p joe.object\_id
> > 
> > #767998854  
> > #8577648
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Wayne\_Brisette](https://avatars.discourse-cdn.com/v4/letter/w/91b2a8/32.png) [@Wayne\_Brisette](https://rubytalk.org/u/Wayne_Brisette)\
**Post date:** [11 April 2013 17:55 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/9 "2013-04-11T17:55:11Z")

</div>

Huh? what are you asking?

> **···**
>
> ----- Original Message -----  
> From: Love U Ruby \<lists@ruby-forum.com\>
> 
> > Now my question is - How does instance variable has been assigned to  
> > `Customer`?

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [11 April 2013 18:14 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/10 "2013-04-11T18:14:47Z")

</div>

The object x that is passed into the block is the class that is being  
created by Struct. When you define x.greeting, this is a singleton  
method on the class object, and when inside that method you call  
"name", you are actually calling a method of the x object, which is  
class. With this test, you have discovered that classes have a "name"  
method, that contains the constant to which they are assigned (this is  
done either when doing class X, or X = \<a class\>):

1.9.2p290 :008 \> Customer.methods  
=\> [...:name,...]

As you can see the class Customer has a name method.

If you create a class without assigning it to a constant, it doesn't  
have a name:

1.9.2p290 :009 \> c = Class.new  
=\> #\<Class:0x00000002519990\>  
1.9.2p290 :010 \> c.name  
=\> nil

on the other hand:

1.9.2p290 :011 \> C = Class.new  
=\> C  
1.9.2p290 :012 \> C.name  
=\> "C"

and

1.9.2p290 :013 \> class D  
1.9.2p290 :014?\> end  
=\> nil  
1.9.2p290 :015 \> D.name  
=\> "D"

Hope this helps,

Jesus.

> **···**
>
> On Thu, Apr 11, 2013 at 7:38 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > Here is another confusion:
> > 
> > I tried the below code:
> > 
> > ===============================  
> > Customer = Struct.new(:name, :address) do |x|  
> > &nbsp;&nbsp;def x.greeting  
> > &nbsp;&nbsp;&nbsp;&nbsp;"Hello #{name}!"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # p Customer.greeting #=\> "Hello Customer!"
> > 
> > Now my question is - How does instance variable has been assigned to  
> > `Customer`?

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 April 2013 21:41 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/11 "2013-04-18T21:41:39Z")

</div>

Love U Ruby wrote in post #1105300:

> ===============================  
> Customer = Struct.new(:name, :address) do |x|  
> &nbsp;&nbsp;def x.greeting  
> &nbsp;&nbsp;&nbsp;&nbsp;"Hello #{name}!"  
> &nbsp;&nbsp;end  
> end
> 
> # p Customer.greeting #=\> "Hello Customer!"

You probably intended to write:

Customer = Struct.new(:name, :address) do  
&nbsp;&nbsp;def greeting  
&nbsp;&nbsp;&nbsp;&nbsp;"Hello #{name}!"  
&nbsp;&nbsp;end  
end

This will create a method named "greeting" on ALL of your Customer  
structs. It's a neat trick, since the method won't be seen as a member  
of the struct.

I like even more that you can include a module:

module Greeter  
&nbsp;&nbsp;def greeting  
&nbsp;&nbsp;&nbsp;&nbsp;"Hello #{name}!"  
&nbsp;&nbsp;end  
end

Customer = Struct.new(:name, :address) do  
&nbsp;&nbsp;include Greeter  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [11 April 2013 11:26 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/12 "2013-04-11T11:26:38Z")

</div>

Remember that all ruby values are objects, even classes.

&nbsp;&nbsp;&nbsp;&nbsp;# classes  
&nbsp;&nbsp;&nbsp;&nbsp;class Foo; end  
&nbsp;&nbsp;&nbsp;&nbsp;Bar = class.new  
&nbsp;&nbsp;&nbsp;&nbsp;$baz = Foo  
&nbsp;&nbsp;&nbsp;&nbsp;qux = Bar

&nbsp;&nbsp;&nbsp;&nbsp;# instance objects  
&nbsp;&nbsp;&nbsp;&nbsp;Foo.new  
&nbsp;&nbsp;&nbsp;&nbsp;Bar.new  
&nbsp;&nbsp;&nbsp;&nbsp;$baz.new  
&nbsp;&nbsp;&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;&nbsp;MyConst =   
&nbsp;&nbsp;&nbsp;&nbsp;# all good:  
&nbsp;&nbsp;&nbsp;&nbsp;MyConst \<\< 1  
&nbsp;&nbsp;&nbsp;&nbsp;MyConst.unshift 7  
&nbsp;&nbsp;&nbsp;&nbsp;MyConst.sort!  
&nbsp;&nbsp;&nbsp;&nbsp;MyConst.delete\_if { |e| e\<3 }  
&nbsp;&nbsp;&nbsp;&nbsp;def MyConst.foo; end  
&nbsp;&nbsp;&nbsp;&nbsp;# not good:  
&nbsp;&nbsp;&nbsp;&nbsp;MyConst = [7]

You can modify the \_object\_, but you can't modify the reference.

> **···**
>
> On Apr 11, 2013 8:19 PM, "Love U Ruby" \<lists@ruby-forum.com\> wrote:
> 
> > "Jesús Gabriel y Galán" \<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:  
> > \>  
> > \*\*\*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.

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [11 April 2013 13:07 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/13 "2013-04-11T13:07:09Z")

</div>

1.9.2p290 :001 \> class S  
1.9.2p290 :002?\> Foo = Class.new  
1.9.2p290 :003?\> end  
=\> S::Foo  
1.9.2p290 :004 \> S::Foo  
=\> S::Foo

A class can have constants inside. Those constants can reference any  
object, for example a Class.  
There's nothing really more to it than this. You refer to a constant  
inside a class with the :: syntax as shown above.

Jesus.

> **···**
>
> On Thu, Apr 11, 2013 at 12:19 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > "Jesús Gabriel y Galán" \<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:
> > 
> > \*\*\*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.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 14:59 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/14 "2013-04-11T14:59:39Z")

</div>

Robert Klemme wrote in post #1105267:

> irb(main):006:0\> 5.times { puts joe.hash }  
> 1060632465  
> 1060632465  
> 1060632465  
> 1060632465  
> 1060632465  
> =\> 5

For me now it is also OK. I don't know why I got such bullshit output  
earlier?

p RUBY\_VERSION  
Customer = Struct.new(:name, :address, :zip)  
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)  
p 5.times { puts joe.hash }  
p joe.hash  
p joe.hash

Output:

"1.9.3"  
-672312861  
-672312861  
-672312861  
-672312861  
-672312861  
5  
-672312861  
8577108  
-672312861  
8577108

> **···**
>
> > On Thu, Apr 11, 2013 at 4:27 PM, Love U Ruby \<lists@ruby-forum.com\> \> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 17:58 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/15 "2013-04-11T17:58:39Z")

</div>

Wayne Brisette wrote in post #1105303:

> **···**
>
> > ----- Original Message -----  
> > From: Love U Ruby \<lists@ruby-forum.com\>
> > 
> > > Now my question is - How does instance variable has been assigned to  
> > > `Customer`?
> > 
> > Huh? what are you asking?
> 
> How "Hello #{name}!" evaluated to "Hello Customer!" ?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 18:57 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/16 "2013-04-11T18:57:46Z")

</div>

"Jesús Gabriel y Galán" \<jgabrielygalan@gmail.com\> wrote in post  
#1105307:

> **···**
>
> > On Thu, Apr 11, 2013 at 7:38 PM, Love U Ruby \<lists@ruby-forum.com\> \> wrote:
> 
> > As you can see the class Customer has a name method.
> > 
> > If you create a class without assigning it to a constant, it doesn't  
> > have a name:
> > 
> > 1.9.2p290 :009 \> c = Class.new  
> > =\> #\<Class:0x00000002519990\>  
> > 1.9.2p290 :010 \> c.name  
> > =\> nil
> > 
> > on the other hand:
> > 
> > 1.9.2p290 :011 \> C = Class.new  
> > =\> C  
> > 1.9.2p290 :012 \> C.name  
> > =\> "C"
> 
> Really a new lesson for me. thank you very much for such an great  
> explanation. At least an impressive difference between `c=Class.new` and  
> `C=Class.new` object instantiation technique.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_Newone](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Alex\_Newone](https://rubytalk.org/u/Alex_Newone)\
**Post date:** [11 April 2013 21:08 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/17 "2013-04-11T21:08:45Z")

</div>

Correct me if i'm wrong, but since when ruby 1.9 uses "name" for class name? I always though that it's Class.new.class.name or Class.class.name that gives proper result.

I've looked at ruby source, and it looks that in ruby 1.9.3 it references module name, but the constant namespace have some side effect on it.

> **···**
>
> On Apr 11, 2013, at 9:14 PM, Jesús Gabriel y Galán wrote:
> 
> > On Thu, Apr 11, 2013 at 7:38 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> > 
> > > Here is another confusion:
> > > 
> > > I tried the below code:
> > > 
> > > ===============================  
> > > Customer = Struct.new(:name, :address) do |x|  
> > > def x.greeting  
> > > &nbsp;&nbsp;&nbsp;"Hello #{name}!"  
> > > end  
> > > end
> > > 
> > > # p Customer.greeting #=\> "Hello Customer!"
> > > 
> > > Now my question is - How does instance variable has been assigned to  
> > > `Customer`?
> > 
> > The object x that is passed into the block is the class that is being  
> > created by Struct. When you define x.greeting, this is a singleton  
> > method on the class object, and when inside that method you call  
> > "name", you are actually calling a method of the x object, which is  
> > class. With this test, you have discovered that classes have a "name"  
> > method, that contains the constant to which they are assigned (this is  
> > done either when doing class X, or X = \<a class\>):
> > 
> > 1.9.2p290 :008 \> Customer.methods  
> > =\> [...:name,...]
> > 
> > As you can see the class Customer has a name method.
> > 
> > If you create a class without assigning it to a constant, it doesn't  
> > have a name:
> > 
> > 1.9.2p290 :009 \> c = Class.new  
> > =\> #\<Class:0x00000002519990\>  
> > 1.9.2p290 :010 \> c.name  
> > =\> nil
> > 
> > on the other hand:
> > 
> > 1.9.2p290 :011 \> C = Class.new  
> > =\> C  
> > 1.9.2p290 :012 \> C.name  
> > =\> "C"
> > 
> > and
> > 
> > 1.9.2p290 :013 \> class D  
> > 1.9.2p290 :014?\> end  
> > =\> nil  
> > 1.9.2p290 :015 \> D.name  
> > =\> "D"
> > 
> > Hope this helps,
> > 
> > Jesus.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [19 April 2013 10:00 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/18 "2013-04-19T10:00:58Z")

</div>

In other words: Struct generated classes are ordinary classes and the block  
passed to Struct.new is a class body as is the one passed to Class.new - no  
surprises here. 🙂

irb(main):001:0\> c=Class.new do  
irb(main):002:1\* def foo; 123 end  
irb(main):003:1\> end  
=\> #\<Class:0x8335bb4\>  
irb(main):004:0\> c.new.foo  
=\> 123

Kind regards

robert

> **···**
>
> On Thu, Apr 18, 2013 at 11:41 PM, Scott Shaffer \<lists@ruby-forum.com\>wrote:
> 
> > I like even more that you can include a module:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 April 2013 15:07 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/19 "2013-04-11T15:07:26Z")

</div>

That output doesn't fit the program. Where do lines with "8577108" come  
from?

Also "p 5.times ..." doesn't make sense.

robert

> **···**
>
> On Thu, Apr 11, 2013 at 4:59 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > Robert Klemme wrote in post #1105267:  
> > \> On Thu, Apr 11, 2013 at 4:27 PM, Love U Ruby \<lists@ruby-forum.com\> \> \> wrote:  
> > \>
> > 
> > \> irb(main):006:0\> 5.times { puts joe.hash }  
> > \> 1060632465  
> > \> 1060632465  
> > \> 1060632465  
> > \> 1060632465  
> > \> 1060632465  
> > \> =\> 5
> > 
> > For me now it is also OK. I don't know why I got such bullshit output  
> > earlier?
> > 
> > p RUBY\_VERSION  
> > Customer = Struct.new(:name, :address, :zip)  
> > joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)  
> > p 5.times { puts joe.hash }  
> > p joe.hash  
> > p joe.hash
> > 
> > Output:
> > 
> > "1.9.3"  
> > -672312861  
> > -672312861  
> > -672312861  
> > -672312861  
> > -672312861  
> > 5  
> > -672312861  
> > 8577108  
> > -672312861  
> > 8577108
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 April 2013 22:57 UTC](https://rubytalk.org/t/confusion-with-struct-class/67690/20 "2013-04-11T22:57:05Z")

</div>

Alex Newone wrote in post #1105332:

> Correct me if i'm wrong, but since when ruby 1.9 uses "name" for class  
> name? I always though that it's Class.new.class.name or Class.class.name  
> that gives proper result.

As Jesus said: "The object x that is passed into the block is the class  
that is being created by Struct."

E.g.:  
&nbsp;&nbsp;irb(main):001:0\> Foo = Struct.new(:a) {|x| p [x, x.class] }  
&nbsp;&nbsp;[#\<Class:0x0000000287a5a8\>, Class]  
&nbsp;&nbsp;=\> Foo

So my `x` is the actual `Foo` class object, not an instance of it. Thus  
the following are effectively equivalent:

&nbsp;&nbsp;Foo = Struct.new(:a) do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;def x.bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{name}bar"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :a  
&nbsp;&nbsp;&nbsp;&nbsp;def Foo.bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{name}bar"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

You can see that `name` is being called on the object on which the  
method Foo is defined, which is the object Foo, which is an instance of  
Class.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

[Next page](https://rubytalk.org/t/confusion-with-struct-class/67690.md?page=2)
