From: Matthew Kerwin Date: 2013-02-08T06:25:04+09:00 Subject: Re: Issues from an extreme beginner --f46d042f95be2cf02304d5291394 Content-Type: text/plain; charset=ISO-8859-1 On 8 February 2013 06:39, Bruce Palmer wrote: > Hassan Schroeder wrote in post #1095774: > > On Wed, Feb 6, 2013 at 10:43 PM, Bruce Palmer > > wrote: > > > >> I don't know what I was thinking before, brain fart perhaps, but I was > >> unnecessarily converting back and forth between string and integer. > > > >> names = name1.length + name2.length + name3.length > > > > Or just: (name1 + name2 + name3).length > > Or: [name1, name2, name3].join.length > > > >> puts 'There are ' + names.to_s + ' character\'s in your name!' > > > > Uh, there's no apostrophe in the plural of "character". FWIW. > > (Sorry, personal pet peeve.) > > I caught this after doing it in, maybe 6 of my prior programs. > > I'm surprised nobody caught that sooner! > Well, we're not marking your assignments for you. ;) Here is another approach that is Rubyish, albeit slightly excessive: [name1, name2, name3].inject(0) {|sum, name| sum + name.length } -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd --f46d042f95be2cf02304d5291394 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 8= February 2013 06:39, Bruce Palmer <lists@ruby-forum.com>= wrote:
Hassan Schroeder wrote in post #1095774:
> On Wed, Feb 6, 2013 at 10:43 PM, Bruce Palmer <lists@ruby-forum.com>
> wrote:
>
>> I don't know what I was thinking before, brain fart perhaps, b= ut I was
>> unnecessarily converting back and forth between string and integer= .
>
>> names =3D name1.length + name2.length + name3.length
>
> Or just: =A0 (name1 + name2 + name3).length
> Or: =A0 =A0 =A0 =A0 =A0[name1, name2, name3].join.length
>
>> puts 'There are ' + names.to_s + ' character\'s in= your name!'
>
> Uh, there's no apostrophe in the plural of "character". = FWIW.
> (Sorry, personal pet peeve.)

I caught this after doing it in, maybe 6 of my prior programs.

I'm surprised nobody caught that sooner!

Well, we're not marking your assignments f= or you.=A0 ;)

Here is another appro= ach that is Rubyish, albeit slightly excessive:

=A0=A0=A0 [name1, name2, name3].inject= (0) {|sum, name| sum + name.length=A0 }

--
=A0 Matthew Kerwin, B.Sc (CompSci) (Hons)
= =A0 http://matt= hew.kerwin.net.au/
=A0 ABN: 59-013-727-651

=A0 "You'll never find a programmin= g language that frees
=A0 you from the burden of clarifying your ideas.&= quot; - xkcd
--f46d042f95be2cf02304d5291394--