How to use include? on an array of objects

Discussion in 'Ruby' started by Fernando Perez, Nov 21, 2008.

  1. I have the following array:

    [ product1, product2, product3 ]

    All products are of type Product. The Product class defines isntance
    attributes such as: title, description, price.

    Can I use include? to check if the array has a product which price is
    zero? if not what would I need to do? Iterate over each product and set
    a variable if a match is found?
    --
    Posted via http://www.ruby-forum.com/.
     
    Fernando Perez, Nov 21, 2008
    #1
    1. Advertising

  2. Fernando Perez

    Shane Emmons Guest

    [Note: parts of this message were removed to make it a legal post.]

    Use the 'select' method of array
    http://ruby-doc.org/core/classes/Array.html#M002214

    On Fri, Nov 21, 2008 at 12:44 PM, Fernando Perez <>wrote:

    > I have the following array:
    >
    > [ product1, product2, product3 ]
    >
    > All products are of type Product. The Product class defines isntance
    > attributes such as: title, description, price.
    >
    > Can I use include? to check if the array has a product which price is
    > zero? if not what would I need to do? Iterate over each product and set
    > a variable if a match is found?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Shane Emmons
     
    Shane Emmons, Nov 21, 2008
    #2
    1. Advertising

  3. Fernando Perez

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    >
    > I have the following array:
    >
    > [ product1, product2, product3 ]
    >
    > All products are of type Product. The Product class defines isntance
    > attributes such as: title, description, price.
    >
    > Can I use include? to check if the array has a product which price is
    > zero? if not what would I need to do?



    You probably want any? :

    product_array.any? { |p| p.price.zero? }
     
    James Coglan, Nov 21, 2008
    #3
  4. Fernando Perez

    Matthew Moss Guest

    On Nov 21, 2008, at 11:44 AM, Fernando Perez wrote:

    > I have the following array:
    >
    > [ product1, product2, product3 ]
    >
    > All products are of type Product. The Product class defines isntance
    > attributes such as: title, description, price.
    >
    > Can I use include? to check if the array has a product which price is
    > zero? if not what would I need to do? Iterate over each product and
    > set
    > a variable if a match is found?


    Given arr = [ product1, product2, product3 ], and you have a 'price'
    method on your products:

    If you want to find the first item of price zero:

    arr.find { |prod| prod.price.zero? }

    If you want to find all items of price zero:

    arr.select { |prod| prod.price.zero? }

    If you just want to know that at least one item has zero price:

    arr.any? { |prod| prod.price.zero? }
     
    Matthew Moss, Nov 21, 2008
    #4
  5. Fernando Perez

    Ryan Davis Guest

    On Nov 21, 2008, at 09:44 , Fernando Perez wrote:

    > I have the following array:
    >
    > [ product1, product2, product3 ]
    >
    > All products are of type Product. The Product class defines isntance
    > attributes such as: title, description, price.
    >
    > Can I use include? to check if the array has a product which price is
    > zero? if not what would I need to do? Iterate over each product and
    > set
    > a variable if a match is found?


    c'mon guys... teach a man to fish.

    Fernando, check out ri. ri is your friend(tm).

    % ri Array
    % ri Enumerable

    (Array includes Enumerable). There is a LOT of good stuff in
    Enumerable. You should poke around and learn as much of it as you can:

    % ri Enumerable.find
    % ri Enumerable.find_all

    etc.

    Other places to look are http://ruby-doc.org/ and the PragProg pickaxe
    book (Programming Ruby, 2nd ed).
     
    Ryan Davis, Nov 21, 2008
    #5
    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. Rolf Magnus
    Replies:
    2
    Views:
    605
    Karl Heinz Buchegger
    Nov 28, 2003
  2. Elie Nader
    Replies:
    1
    Views:
    642
  3. Tuckers
    Replies:
    18
    Views:
    624
    ThosRTanner
    May 18, 2005
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    935
    Andreas Bogenberger
    Feb 22, 2008
  5. Fredxx
    Replies:
    3
    Views:
    648
    Martin Thompson
    Jul 15, 2009
Loading...

Share This Page