From: Ryan Cook Date: 2013-03-02T04:00:16+09:00 Subject: Re: Why class returns `null` but the definition not when their description ended in IRB? --f46d0444ec6352fdf904d6e18197 Content-Type: text/plain; charset=ISO-8859-1 Oops! Right on Matt -- beat me to it. On Fri, Mar 1, 2013 at 11:51 AM, Ryan Cook wrote: > When a class or module is defined, the code inside is evaluated and the > return value of the last expression is returned. This is because you could > define class level attributes or constants or execute arbitrary code > relevant to the class or to prepare it. > > When a method is defined as in your second example, the code inside is > only executed when the method is run. You could call the D() method to > return 8 as expected. > > Is that helpful? > > > On Fri, Mar 1, 2013 at 11:38 AM, Matt Mongeau wrote: > >> Because that's how it is implemented. >> >> >> 2013/3/1 Kumar R. >> >> The below class description returns its last evaluated expression : >>> >>> >> class A >>> >> 1+7 >>> >> 2+4 >>> >> end >>> => 6 >>> >>> To return `6` which method the expression generally called? >>> >>> But why the definition always `nil` ? >>> >>> >> def D >>> >> 3+5 >>> >> end >>> => nil >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> >>> >> > > > -- > Ryan Cook > 720.319.7660 > -- Ryan Cook 720.319.7660 --f46d0444ec6352fdf904d6e18197 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Oops! Right on Matt -- beat me to it.


On Fri, Mar 1, 2013 at 11:51= AM, Ryan Cook <cookrn@gmail.com> wrote:
When a class or module is d= efined, the code inside is evaluated and the return value of the last expre= ssion is returned. This is because you could define class level attributes = or constants or execute arbitrary code relevant to the class or to prepare = it.

When a method is defined as in your second example, the= code inside is only executed when the method is run. You could call the D(= ) method to return 8 as expected.

Is that helpful?


On Fri, Mar 1, 2013 at 11:38 AM, Matt= Mongeau <halogenandtoast@gmail.com> wrote:
Because that's how it i= s implemented.


2013/3/1 Kumar R. <lists@ruby-forum.com>

The below class description returns its last= evaluated expression :

>> class A
>> 1+7
>> 2+4
>> end
=3D> 6

To return `6` which method the expression generally called?

But why the definition always `nil` ?

>> def D
>> 3+5
>> end
=3D> nil

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





--
Ryan Cook
720.319.7660



--
Ryan Cook720.319.7660
--f46d0444ec6352fdf904d6e18197--