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. Advertising

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:
    366
    Harald
    Jun 12, 2005
  2. Siemel Naran

    typos in set functions

    Siemel Naran, Nov 30, 2004, in forum: C++
    Replies:
    5
    Views:
    362
    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:
    1,581
    David Bolen
    Jul 29, 2003
  4. Bob Gailer
    Replies:
    2
    Views:
    406
    Bengt Richter
    Jul 26, 2003
  5. Mauricio Fernandez
    Replies:
    0
    Views:
    170
    Mauricio Fernandez
    Jan 5, 2007
Loading...

Share This Page