From: Chad Perrin Date: 2011-10-19T03:15:27+09:00 Subject: Re: Return --uAKRQypu60I7Lcqm Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Oct 19, 2011 at 02:26:54AM +0900, Junayeed Ahnaf Nirjhor wrote: >=20 > What does return do? I mean, it returns something, then why does it vary = from puts? (1+1) That expression returns a value of 2. foo =3D (1+1) That captures the return value of 2 in a variable called foo. puts (1+1) That prints the return value of 2 as a string to standard output, and adds a newline to the end of it. foo =3D (1+1) That, too, is an expression with a return value. The return value happens to be the same value captured in the variable foo, so once again the return value of 2 applies. puts (1+1) The puts method has a return value of nil, so that expression returns a value of nil. foo =3D puts (1+1) Here, the nil return value of the puts method is captured in the foo variable, which then returns a value of nil. If you use irb, it looks a bit like this: $ irb >> (1+1) =3D> 2 >> foo =3D (1+1) =3D> 2 >> puts (1+1) 2 =3D> nil >> foo =3D puts (1+1) 2 =3D> nil Those lines that start with >> are the prompts where I entered Ruby expressions. The lines that start with =3D> show the return values of those expressions when they are executed. The lines that show nothing but 2 are the lines that show what puts sends to standard output. Here's a little more irb experimentation on the subject: >> puts puts (1+1) 2 =3D> nil >> puts (puts (1+1)).inspect 2 nil =3D> nil I hope that helps. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --uAKRQypu60I7Lcqm Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk6dweYACgkQ9mn/Pj01uKXLVwCg5DbTaSfZZQMFlB4reXAX3VU4 +REAmgIiyCZRTnN4Jd5gF0eX9hdTvEtA =OMXl -----END PGP SIGNATURE----- --uAKRQypu60I7Lcqm--