Forum: Ruby Why class returning its own name when "include" statement?

Posted by Love U Ruby (my-ruby)
on 2013-02-23 08:03
I was actually playing around with the class definition return values.
below is the all IRB code that I used till now.

module Show
def show_string
p "hi"
end
end

#=> nil
class Test
include Show
def show_line
p "I am a boy"
end
end
#=> nil

class Abc
end
#=> nil
class Bbc
@X=10
end
#=> 10
class Test123
include Show
@x=5
end
#=> 5

=========================================================================
Till the above code I did understand the logic,but in the below code why
"class" definition returns its own name whenever "include" is the last
statement? - I didn't understand this fact.
=========================================================================

class Foo
@x=12
include Show
end
#=> Foo


class Test
include Show
end
#=> Test
Posted by Stefano Crocco (crocco)
on 2013-02-23 09:04
(Received via mailing list)
On Saturday 23 February 2013 Love U Ruby wrote
> class Test
> class Bbc
> Till the above code I did understand the logic,but in the below code why
>
> class Test
> include Show
> end
> #=> Test
>
> --
> Posted via http://www.ruby-forum.com/.

As you can see from your Abc example, a class expression returns the 
value of
the last expression inside it. Since Module#include returns self (that 
is, the
caller which, in your case, is class Foo), the whole class expression 
returns
the class itself (not its name, but the class object).

Stefano
Posted by Love U Ruby (my-ruby)
on 2013-02-23 09:20
@Stefano Yes you are right. The below code is proved that.

*********************************************************
 module Show
 def show_string
 p "hi"
 end
 end
# => nil
  class Foo
  @x=12
  (include Show).object_id
  end
# => 10681980
  class Too
  @x=12
  (include Show).object_id.inspect
  end
# => "10648020"
*********************************************************

Does that mean @x has been already initialized? or curious to know what 
"10648020" contains into it?
Posted by Love U Ruby (my-ruby)
on 2013-02-24 17:15
Can anyone help me with my above question please?
Posted by tamouse mailing lists (Guest)
on 2013-02-24 17:59
(Received via mailing list)
On Feb 23, 2013 2:21 AM, "Love U Ruby" <lists@ruby-forum.com> wrote:
>   class Foo
>
> Does that mean @x has been already initialized? or curious to know what
> "10648020" contains into it?

@x is set inside the class and will be set thus when Foo.new or Too.new 
is
called. Each such object will have its own copy of @x.

The number in quotes is the inspected object id of the included Show 
module
instance.
Posted by Love U Ruby (my-ruby)
on 2013-02-24 18:02
tamouse mailing lists wrote in post #1098808:
> On Feb 23, 2013 2:21 AM, "Love U Ruby" <lists@ruby-forum.com> wrote:
>>   class Foo
>>
>> Does that mean @x has been already initialized? or curious to know what
>> "10648020" contains into it?
>
> @x is set inside the class and will be set thus when Foo.new or Too.new
> is
> called. Each such object will have its own copy of @x.
>
> The number in quotes is the inspected object id of the included Show
> module
> instance.

not sure about your statement. seems @ Stefano statement contradicts 
with you.

Can it be more specific what you meant to say?
Posted by tamouse mailing lists (Guest)
on 2013-02-24 18:17
(Received via mailing list)
On Feb 24, 2013 11:05 AM, "Love U Ruby" <lists@ruby-forum.com> wrote:
> > called. Each such object will have its own copy of @x.
> >
> > The number in quotes is the inspected object id of the included Show
> > module
> > instance.
>
> not sure about your statement. seems @ Stefano statement contradicts
> with you.
>
> Can it be more specific what you meant to say?
>

They are not inconsistent at all. In each,the result of the last 
expression
evaluated is returned.  In the last case, you evaluated (include
Show).object_id.inspect, didn't you?
Posted by Love U Ruby (my-ruby)
on 2013-02-24 18:29
tamouse mailing lists wrote in post #1098812:
> On Feb 24, 2013 11:05 AM, "Love U Ruby" <lists@ruby-forum.com> wrote:
>> > called. Each such object will have its own copy of @x.

>>
>
> They are not inconsistent at all. In each,the result of the last
> expression
> evaluated is returned.  In the last case, you evaluated (include
> Show).object_id.inspect, didn't you?

Yes, I did to see, if object created at the "include" time or not. And 
it shows "yes" the object created at class definition time.

Then my above list mentioned questions came into my mind.


Thanks
Posted by unknown (Guest)
on 2013-02-24 20:38
(Received via mailing list)
Am 23.02.2013 09:20, schrieb Love U Ruby:
>    @x=12
> Does that mean @x has been already initialized? or curious to know what
> "10648020" contains into it?

It's useless to ask these kind of "academic", pointless questions as
long as you do not even have the most basic knowledge about how to
properly define a class with an instance variable.

Read for example this:

   http://pine.fm/LearnToProgram/

Read it from beginning to end, do *all* the exercises.

After that, choose a programming project to work on, something
with a purpose. Then ask questions about how to achieve your goal.
Posted by Henry Maddocks (Guest)
on 2013-02-24 21:40
(Received via mailing list)
On 25/02/2013, at 8:38 AM, sto.mar@web.de wrote:
>
> After that, choose a programming project to work on, something
> with a purpose. Then ask questions about how to achieve your goal.

Sounds to me like he/she is trying to write a ruby implementation.

Henry
Posted by Ryan Davis (Guest)
on 2013-02-24 21:46
(Received via mailing list)
On Feb 24, 2013, at 12:39, Henry Maddocks <hmaddocks@me.com> wrote:

> On 25/02/2013, at 8:38 AM, sto.mar@web.de wrote:
>>
>> After that, choose a programming project to work on, something
>> with a purpose. Then ask questions about how to achieve your goal.
>
> Sounds to me like he/she is trying to write a ruby implementation.

Very very poorly in the most annoying and least accurate way possible.

I do wish we'd collectively stop responding to our latest Illias.
Posted by tamouse mailing lists (Guest)
on 2013-02-24 22:34
(Received via mailing list)
On Sun, Feb 24, 2013 at 2:45 PM, Ryan Davis <ryand-ruby@zenspider.com> 
wrote:
> I do wish we'd collectively stop responding to our latest Illias.

Well, okay, if that's how others feel, as well.
Posted by Marc Heiler (shevegen)
on 2013-02-24 23:27
>> I do wish we'd collectively stop responding to our latest Illias.

> Well, okay, if that's how others feel, as well.

It is true. :)

He is Illias 2.0
Posted by Love U Ruby (my-ruby)
on 2013-02-25 09:43
Ryan Davis wrote in post #1098842:
> On Feb 24, 2013, at 12:39, Henry Maddocks <hmaddocks@me.com> wrote:
>
>> On 25/02/2013, at 8:38 AM, sto.mar@web.de wrote:

>
> I do wish we'd collectively stop responding to our latest Illias.

What happened Ryan with you ? Why are you forcing others to believe your 
thought about me?

This way nothing gonna be helpful. please don't do that.

People who has knowledge, no way you can't stop them to share it. 
Because knowledge sharing has some innate proudest within it. If you 
don't like answers to the conceptual questions, only programming type of 
questions get yourself busy on that. Don't need to look into the 
question who is asking conceptual things.

As to get into and discover concept from the existing one is another 
kind of fascination I think.

Someone might read Array#[] the way it has been documented, But I love 
to think beyond that. And If there I feel I need a bit help to move 
forward I asked here. Why you think this is for only my personal use 
only. Every new started should think this way and If they got stuck can 
take my ones as their references.

If I ask only software development specific questions then it would 
might be helpful for part of ruby users but the conceptual questions are 
for all.

Again If you feel bad, please don't need to answer my ones,but 
requesting you also to that don't say others to follow you.

Try to understand your weight.

Thanks
Posted by Love U Ruby (my-ruby)
on 2013-02-25 09:57
Love U Ruby wrote in post #1098555:
 @Stefano Yes you are right. The below code is proved that.

*********************************************************
  module Show
 def show_string
  p "hi"
  end
  end
# => nil
  class Foo
  @x=12
  (include Show).object_id
   end
 # => 10681980
   class Too
  @x=12
  (include Show).object_id.inspect
   end
# => "10648020"
 *********************************************************

As here I didn't create any object of `Too` and `Foo`,then how does such
object number come ?
Posted by D. Deryl Downey (ddd)
on 2013-02-25 16:15
(Received via mailing list)
The group has collectively informed you that you will no longer be
answered for any further questions. This decision was made based upon 
your
inability to follow directions, your willful lack at attempting to 
learn,
your seemingly willful disregard for using ANY of the tutorials, URLs, 
or
other educational material provided to you, and your clear disregard of
instructions not to use the mailing list for every single solitary 
thought
that crosses through your mind, as well as your willful disregard for
Googling and learning on your own.

You have zero purpose, you have zero willingness to self educate and
study, and zero willingness to work within the confines of the group's
requirements which is to study, self educate, try, retry, play around 
with
various code and compare your results to known documentation (of which
there is plenty, both that provided by us to you, and that which Google
provides (not to mention ruby-lang.org)).

Your failure to follow our most basic tenants has resulted in us
collectively refusing to answer any further questions that you submit. I
suggest you quit posting for awhile and do some real studying and some
real thinking on proper etiquette and usage of mailing lists. You *will*
find the same response given on all Ruby related mailing lists from this
point forward.

You did this to yourself.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.