--sch5K7FaKZtykyec
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Dmitri Colebatch (dim / colebatch.com) wrote:

> how should I be doing the above?  I ended up doing this:
>=20
>   def foo(foobar)
>     bar =3D
>       if foobar !=3D nil
>         bar =3D foobar * 4
>       else
>         bar =3D "foobar!"
>       end
>     bar
>   end
>=20
> which worked fine, but I was wondering if there was a way to do what I=20
> was trying initially.

Yes, the value of the last statement in an if/elsif/else/end block is
the value of the if :)  So you can just do this:

def foo(foobar)
  bar =3D
   if foobar !=3D nil
     foobar * 4
   else
     bar =3D "foobar!"
   end
  end
end
=20
Or even shorter:

def foo(foobar)
  if foobar !=3D nil
    foobar * 4
  else
    "foobar!"
  end
end

Or you can go all C-style and do:

def foo(foobar) foobar.nil? ? "foobar" : foobar * 4 end

--=20
Eric Hodel - drbrain / segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04


--sch5K7FaKZtykyec
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (FreeBSD)

iD8DBQE+LOdPMypVHHlsnwQRAqA9AJ0XM8lWlWzLPed8JL176njIsD6llQCgio0u
NEolZRSlfNPchU2hBxwlpwI=
=n7+J
-----END PGP SIGNATURE-----

--sch5K7FaKZtykyec--

Dmitri Colebatch (dim / colebatch.com) wrote:

> how should I be doing the above?  I ended up doing this:
>=20
>   def foo(foobar)
>     bar =3D
>       if foobar !=3D nil
>         bar =3D foobar * 4
>       else
>         bar =3D "foobar!"
>       end
>     bar
>   end
>=20
> which worked fine, but I was wondering if there was a way to do what I=20
> was trying initially.

Yes, the value of the last statement in an if/elsif/else/end block is
the value of the if :)  So you can just do this:

def foo(foobar)
  bar =3D
   if foobar !=3D nil
     foobar * 4
   else
     bar =3D "foobar!"
   end
  end
end
=20
Or even shorter:

def foo(foobar)
  if foobar !=3D nil
    foobar * 4
  else
    "foobar!"
  end
end

Or you can go all C-style and do:

def foo(foobar) foobar.nil? ? "foobar" : foobar * 4 end

--=20
Eric Hodel - drbrain / segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (FreeBSD)

iD8DBQE+LOdPMypVHHlsnwQRAqA9AJ0XM8lWlWzLPed8JL176njIsD6llQCgio0u
NEolZRSlfNPchU2hBxwlpwI=
=n7+J
-----END PGP SIGNATURE-----