Typos in eigenclass - Changes in Ruby 1.9

Discussion in 'Ruby' started by Wolfgang Nádasi-donner, Aug 3, 2007.

  1. Moin, moin!

    I didn't find a way to post this in http://eigenclass.org, so I put it
    here.

    In http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l54
    (Enumerable#count) the following code was listed:

    def count(*a)
    inject(0) do |c, e|
    unless a.size == 1 # suspect, but this is how it works
    (a[0] == x) ? c + 1 : c
    else
    yield(x) ? c + 1 : c
    end
    end
    end

    with the expected result:

    ["bar", 1, "foo", 2].count(1) # => 1
    ["bar", 1, "foo", 2].count{|x| x.to_i != 0} # => 2

    This doesn't work, because of the variable "x" and "unless" instead of
    "if". After the following changes it works as expected:

    module Enumerable
    def count(*a)
    inject(0) do |c, e|
    if a.size == 1
    (a[0] == e) ? c + 1 : c
    else
    yield(e) ? c + 1 : c
    end
    end
    end
    end

    p ["bar", 1, "foo", 2].count(1) # => 1
    p ["bar", 1, "foo", 2].count{|x| x.to_i != 0} # => 2

    Wolfgang Nádasi-Donner
    --
    Posted via http://www.ruby-forum.com/.
     
    Wolfgang Nádasi-donner, Aug 3, 2007
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dagoodyear

    Parsing text acounting for typos?

    dagoodyear, Jun 12, 2005, in forum: Java
    Replies:
    1
    Views:
    527
    Harald
    Jun 12, 2005
  2. Siemel Naran

    typos in set functions

    Siemel Naran, Nov 30, 2004, in forum: C++
    Replies:
    5
    Views:
    474
    Siemel Naran
    Dec 2, 2004
  3. Manish Jethani

    How to detect typos in Python programs

    Manish Jethani, Jul 25, 2003, in forum: Python
    Replies:
    15
    Views:
    2,167
    David Bolen
    Jul 29, 2003
  4. Bob Gailer
    Replies:
    2
    Views:
    553
    Bengt Richter
    Jul 26, 2003
  5. Peter v.d. Berger

    Matching filenames with typos

    Peter v.d. Berger, Dec 4, 2006, in forum: Perl
    Replies:
    1
    Views:
    1,991
    Jim Gibson
    Dec 5, 2006
  6. Mauricio Fernandez
    Replies:
    0
    Views:
    292
    Mauricio Fernandez
    Jan 5, 2007
  7. Hendrik Maryns

    regexp inside <> + typos in perldoc

    Hendrik Maryns, Jan 31, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    204
    Hendrik Maryns
    Feb 2, 2005
  8. Andreas Leitgeb

    Fixing "typos" in enums...

    Andreas Leitgeb, Nov 15, 2013, in forum: Java
    Replies:
    18
    Views:
    460
    Andreas Leitgeb
    Nov 19, 2013
Loading...