3 Replies - 661 Views - Last Post: 07 March 2011 - 06:45 AM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Creating a program to compare classes

Posted 06 March 2011 - 09:39 AM

This is my first time using Ruby so I need some help. The point of this program is to take elements listed in an array and compare the classes of each element. If the classes are all the same return true else return false. I run it and nothing happens.. not sure where to go from here. Please help!

#adding two elements to class Array; homog? and nhomog?

class Array
  def homog?
    while(self.each) 
      i = 0
      if(self[i].class = self[i+1].class)
        return true
      else
        return false
      end
      i = i + 1
    end
  end
end

#test rounds
print [5,9,1].homog?
#print ["this","that","Mike"].homog?
#print[4,"Trees"].homog?
#print[].homog?
#print[4,7,nil,10,nil,2].homog?
#print[5,5.1,7.98].homog?


# Wait for the user to hit enter
gets  


This post has been edited by SnoBunny85: 06 March 2011 - 09:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a program to compare classes

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Creating a program to compare classes

Posted 06 March 2011 - 12:18 PM

Well first thing I think you meant == and not simply =. Secondly, you return from the function after just the first iteration. Whether the two are equal or not, you return which exits the function and will never even get to the i = i +1 part. Third, if you are looping on each, it will go through every element of the array, even the last one so i+1 is going to be too far off the end of the array. Fourth, you put i = 0 inside the while loop which means it resets i each time back to zero.

What you may want to do is something like this...

class Array
  def homog?
    # i is set once here
    i = 0

    # Loop while the counter is less than last index (length - 1)
    while(i < (self.length - 1)) 
      # Only exit false if the two are not matching
      if(self[i].class != self[i+1].class)
        return false
      end
      i = i + 1
    end
    
    # If we reach here it means that all in the array are equal
    return true
  end
end



So as you can see we loop using the counter and making sure we only exit false if one is not equal to the other. Otherwise we return true to say are all the same. Keep in mind this would return true if the list is empty which may or may not be what you want. Is a list of nothing homogeneous?

So enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Creating a program to compare classes

Posted 06 March 2011 - 06:26 PM

what would you do if you wanted to ignore nil classes... i tried this

if (self[i].class == NilClass
)
   return true



i put it right inside my while statement before the other if statement... thanks for all the help

This post has been edited by SnoBunny85: 06 March 2011 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: Creating a program to compare classes

Posted 07 March 2011 - 06:45 AM

View PostSnoBunny85, on 06 March 2011 - 08:26 PM, said:

what would you do if you wanted to ignore nil classes... i tried this

if (self[i].class == NilClass
)
   return true



i put it right inside my while statement before the other if statement... thanks for all the help

self[i].nil?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1