On 3/9/12 4:09 PM, H. S. Teoh wrote:
On Fri, Mar 09, 2012 at 01:34:38PM -0500, Nick Sabalausky wrote:
"Ary Manzana"<a...@esperanto.org.ar>  wrote in message
news:jjd21r$6ni$1...@digitalmars.com...

Sample Ruby session:

irb
ruby-1.8.7-p352 :001>  [1, 2, 3].count
  =>  3
ruby-1.8.7-p352 :002>  [1, 2, 3].length
  =>  3
ruby-1.8.7-p352 :003>  [1, 2, 3].size
  =>  3

I never saw *anyone* complaining about this. When you write, you
choose whatever is convenient to you (whatever comes to your mind
first). When you read it, it's understandable. Nobody wonders "why
didn't he wrote 'length' instead of 'size'", because the meaning is
clear.

I would wonder what the subtle distinction is. FWIW.
[...]

Me too. I would assume that 'count' counts array elements whereas 'size'
counts the number of bytes the array uses up.

Indeed, count can be used to count elements:

ruby-1.8.7-p352 :002 > [1, 2, 3, 3, 3].count 3
 => 3
ruby-1.8.7-p352 :004 > [1, 2, 3, 3, 3].count &:odd?
 => 4
ruby-1.8.7-p352 :005 > [1, 2, 3, 3, 3].count { |x| x <= 2 }
 => 2

Just that when you use it without arguments it just counts everything.

size is an alias of length. But count just work as an alias as well, without arguments.


IMAO, this sort of "write your mind and somehow it just works" thing
only encourages lazy programming (guessing what something does without
knowing for sure, and copy-n-pasting code without understanding it,
which leads to bit rot and hideous patchwork code that houses all sorts
of subtle bugs and corner-case failures).

We also write lots of tests in Ruby. :-P

Reply via email to