Identifying

Discussion in 'Ruby' started by Adam Akhtar, Feb 9, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    If you have an array which contains a mixture of say intergers and
    nested arrays, how can you determine whether an element is an array or
    not

    ie in psedo code

    array.each do |x|
    if x == Array
    do something
    end

    whats the corect way to write the if statement?

    Thanks

    Adam.
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 9, 2008
    #1
    1. Advertising

  2. Adam Akhtar

    Adam Akhtar Guest


    > ie in psedo code
    >
    > array.each do |x|
    > if x == Array
    > do something
    > end
    >
    > whats the corect way to write the if statement?
    >
    > Thanks
    >
    > Adam.


    in the above array = [1,2,3,[34,56,43,7],6,7] etc
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 9, 2008
    #2
    1. Advertising

  3. Adam Akhtar

    PullMonkey Guest

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

    if x.class == Array
    or
    if x.is_a?(Array)

    irb(main):008:0> [1,2,3,[34,56,43,7],6,7].map{|x| x.is_a?(Array)}
    => [false, false, false, true, false, false]


    On Feb 9, 2008 3:01 PM, Adam Akhtar <> wrote:

    >
    > > ie in psedo code
    > >
    > > array.each do |x|
    > > if x == Array
    > > do something
    > > end
    > >
    > > whats the corect way to write the if statement?
    > >
    > > Thanks
    > >
    > > Adam.

    >
    > in the above array = [1,2,3,[34,56,43,7],6,7] etc
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    PullMonkey, Feb 9, 2008
    #3
  4. Alle Saturday 09 February 2008, Adam Akhtar ha scritto:
    > If you have an array which contains a mixture of say intergers and
    > nested arrays, how can you determine whether an element is an array or
    > not
    >
    > ie in psedo code
    >
    > array.each do |x|
    > if x == Array
    > do something
    > end
    >
    > whats the corect way to write the if statement?
    >
    > Thanks
    >
    > Adam.


    if x.is_a? Array

    Stefano
     
    Stefano Crocco, Feb 9, 2008
    #4
  5. On 10/02/2008, Adam Akhtar <> wrote:
    > If you have an array which contains a mixture of say intergers and
    > nested arrays, how can you determine whether an element is an array or
    > not
    >
    > ie in psedo code
    >
    > array.each do |x|
    > if x == Array
    > do something
    > end
    >
    > whats the corect way to write the if statement?


    Use a case statement:

    array.each do |x|
    case x
    when Array: do something
    when Integer: do something
    end
    end

    Farrel
     
    Farrel Lifson, Feb 9, 2008
    #5
  6. Adam Akhtar

    Adam Akhtar Guest

    Farrel Lifson wrote:

    > Use a case statement:
    >
    > array.each do |x|
    > case x
    > when Array: do something
    > when Integer: do something
    > end
    > end


    is

    when Array: do something

    a valid statement??

    is

    if x == Array

    also a valid statement??

    i was expecting having to use something like x.is_a as Stefano said.
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 9, 2008
    #6
  7. Adam Akhtar

    Tim Hunter Guest

    Adam Akhtar wrote:
    > Farrel Lifson wrote:
    >
    >> Use a case statement:
    >>
    >> array.each do |x|
    >> case x
    >> when Array: do something
    >> when Integer: do something
    >> end
    >> end

    >
    > is
    >
    > when Array: do something
    >
    > a valid statement??
    >
    > is
    >
    > if x == Array
    >
    > also a valid statement??
    >
    > i was expecting having to use something like x.is_a as Stefano said.



    when Array:

    is valid in the context of a case expression.

    if x == Array

    is also valid, but will only be true if x is in fact Array, that is

    x = Array
    if x == Array # will be true

    For all other things, it will be false.

    Here's an online copy of the 1st Edition of _Programming_Ruby_, which
    will help answer these questions:
    http://www.ruby-doc.org/docs/ProgrammingRuby/


    --
    RMagick: http://rmagick.rubyforge.org/
    RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
     
    Tim Hunter, Feb 9, 2008
    #7
  8. On Feb 9, 2008 2:00 PM, Adam Akhtar <> wrote:
    > If you have an array which contains a mixture of say intergers and
    > nested arrays, how can you determine whether an element is an array or
    > not
    >
    > ie in psedo code
    >
    > array.each do |x|
    > if x == Array
    > do something
    > end
    >
    > whats the corect way to write the if statement?



    Depending on exactly what you want there are two main ways (with
    several different ways of expressing the most common):

    if x.instance_of? Array ... # tests if x is an Array (strictly)
    if x.kind_of? Array ... # test if x is an instance of Array or one of
    its descendants
    if x.is_a? Array ... # same as kind_of?
    if Array === x ... # same as kind_of?, useful to know exists because
    it lets you match against classes in case statements
     
    Christopher Dicely, Feb 9, 2008
    #8
  9. On Feb 9, 2008 2:14 PM, Adam Akhtar <> wrote:
    > Farrel Lifson wrote:
    >
    > > Use a case statement:
    > >
    > > array.each do |x|
    > > case x
    > > when Array: do something
    > > when Integer: do something
    > > end
    > > end

    >
    > is
    >
    > when Array: do something
    >
    > a valid statement??


    Yes. "when" uses the === method of the object given, and Array is a
    constant instance of class Class, and Class#===(obj) (inherited from
    Module) is equivalent to obj.kind_of?(self)

    >
    > is
    >
    > if x == Array
    >
    > also a valid statement??


    x == Array is a *valid* statement, but it doesn't mean the same thing.

    case x
    when Array: ...
    end

    is the same as:

    if Array === x then ... end

    not:

    if x == Array then ... end

    > i was expecting having to use something like x.is_a as Stefano said.


    As is oft the case with Ruby, there is more than one way to do it.
     
    Christopher Dicely, Feb 9, 2008
    #9
  10. Adam Akhtar

    Adam Akhtar Guest

    thats brilliant. ive never seen === before. I thought it was a typo at
    first. Ill look into those methods now. Thanks


    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 10, 2008
    #10
    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. Dinesh Upare
    Replies:
    0
    Views:
    335
    Dinesh Upare
    Nov 7, 2003
  2. =?Utf-8?B?UGF1bCBL?=

    Identifying calls from anchors

    =?Utf-8?B?UGF1bCBL?=, Jun 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    339
    =?Utf-8?B?UGF1bCBL?=
    Jun 18, 2004
  3. Jeff S
    Replies:
    18
    Views:
    612
    Kevin Spencer
    Jul 12, 2004
  4. kw
    Replies:
    9
    Views:
    2,938
  5. thomson

    Identifying the new Session ID

    thomson, Oct 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    353
    Patrice
    Oct 6, 2005
Loading...

Share This Page