"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:
"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
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:
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>):
===============================
Customer = Struct.new(:name, :address) do |x|
def x.greeting
"Hello #{name}!"
end
end
p Customer.greeting #=> "Hello Customer!"
You probably intended to write:
Customer = Struct.new(:name, :address) do
def greeting
"Hello #{name}!"
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
def greeting
"Hello #{name}!"
end
end
Customer = Struct.new(:name, :address) do
include Greeter
end
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.
···
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.
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.
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.
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
"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>):
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:
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."