Confusion with Struct class

I went to there - Class: Struct (Ruby 2.0.0) but the
below mentioned not cleared.

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

Struct.new(“Customer”, :name, :address)
p Customer.class #`': uninitialized constant Customer (NameError)
Again the below works:
p Struct::Customer.class #=>class

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

On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby [email protected]
wrote:

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.

“Jesús Gabriel y Galán” [email protected] wrote in post
#1105232:

On Thu, Apr 11, 2013 at 10:45 AM, Love U Ruby [email protected]
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.

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

On Thu, Apr 11, 2013 at 12:19 PM, Love U Ruby [email protected]
wrote:

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.

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 Apr 11, 2013 8:19 PM, “Love U Ruby” [email protected] wrote:

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 = class.new
$baz = Foo
qux = 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 = []
# all good:
MyConst << 1
MyConst.unshift 7
MyConst.sort!
MyConst.delete_if { |e| e<3 }
def MyConst.foo; end
# not good:
MyConst = [7]

You can modify the object, but you can’t modify the reference.

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

On 11/04/2013, at 11:59 PM, Love U Ruby [email protected] wrote:

with the above two approaches?

Thanks


Posted via http://www.ruby-forum.com/.

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
=> #

Julian

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 S.”, “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

On Thu, Apr 11, 2013 at 4:27 PM, Love U Ruby [email protected]
wrote:

#-93880937

#767998854
#8577648

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 S.”, “123 Maple, Anytown
NC”,
12345)
=> #<struct Customer name=“Joe S.”, 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 S.”, “123 Maple,
Anytown NC”, 12345).hash }
1060632465
1060632465
1060632465
1060632465
1060632465
=> 5

robert

On Thu, Apr 11, 2013 at 4:59 PM, Love U Ruby [email protected]
wrote:

1060632465
p joe.hash
-672312861
8577108
-672312861
8577108

That output doesn’t fit the program. Where do lines with “8577108” come
from?

Also “p 5.times …” doesn’t make sense.

robert

Robert K. wrote in post #1105270:

On Thu, Apr 11, 2013 at 4:59 PM, Love U Ruby [email protected]
wrote:

1060632465

p joe.hash
-672312861
8577108
-672312861
8577108

That output doesn’t fit the program. Where do lines with “8577108” come
from?

That was a typo. Actually I used in the code Object_id. those numbers
are object_id of Joe. But during copy -paste I forgot to remove those
numbers. Sorry for that.

Could you give me a valid example? on the below -

eql?(other) → true or false

The way you’re teaching yourself ruby isn’t good.

It’s better to have an intention… a real world use… for example, try
to build yourself a text based adventure game… don’t just go through
all the methods and ask us what they do. You won’t learn very well that
way, and nothing will stick in your mind.

Julian

An alternative way to learn ruby would be to go and do the ruby koans.

Julian

Robert K. wrote in post #1105267:

On Thu, Apr 11, 2013 at 4:27 PM, Love U Ruby [email protected]
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 S.”, “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

Here is another confusion:

I tried the below code:

===============================
Customer = Struct.new(:name, :address) do |x|
def x.greeting
“Hello #{name}!”
end
end

p Customer.greeting #=> “Hello Customer!”

Now my question is - How does instance variable has been assigned to
Customer?

----- Original Message -----
From: Love U Ruby [email protected]

Now my question is - How does instance variable has been assigned to
Customer?

Huh? what are you asking?

Julian L. wrote in post #1105279:

An alternative way to learn ruby would be to go and do the ruby koans.

Julian

Yes,you are right. Thanks for the suggestions. But I am making myself
educated on each class and the methods of that class. Otherwise I have
seen sometimes I write a complex code to get something,but ruby has a
built function to do that. Thus I think it is good to touch all the
methods at-least. So that wright things can be used at wright time.

Finally I got it as below:

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new(“Joe S.”, “123 Maple, Anytown NC”, 12345)
jon = Customer.new(“Joe S.”, “123 Maple, Anytown NC”, 12345)
p jon.eql? joe #=> true
jon = Customer.new(“Joe Sen”, “123 Maple, Anytown NC”, 12345)
p jon.eql? joe #=> false

Wayne B. wrote in post #1105303:

----- Original Message -----
From: Love U Ruby [email protected]

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!” ?

On Thu, Apr 11, 2013 at 7:38 PM, Love U Ruby [email protected]
wrote:

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 = ):

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.

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.