• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking an element out of an array  RSS feed

 
Ron Temple
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to take an animal element of an array for filtering but I'm having some problems. I need to take the element i need out of the array. Then I need to find out whether the animal is the type I need. Finally I need to add the animal object to the array. I have to seperate arrays of displayed animals and all the animals but I'm just having trouble figuring out how to do the above. Any advice is greatly appreciated. Here's what I have so far:



And the entire code:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm lost: what are you trying to do again?
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron,
try using

You could even pass the class type into a function and reduce the duplication in the code.
I have come up against this sort of issue before and never really had the time to try to figure out why this is happening. I have always just gone with the isInstance() function instead. If somebody can explain it I would be grateful.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Allain: you're saying instanceOf doesn't work for you?

@Ron: The fragment, and that fragment embedded in the entire code, are not the same. What *is* happening, and what do you *expect* to happen?
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
What I am saying is that I have tried to use the instanceof before and I must have made a mistake with it, or not understood how it works. At the time instead of using instanceof I used class.isInstance(o) and achieved the outcome I was looking for.
I didn't have time to examine what I was doing wrong, and it fell of the radar. I now have the time to do that now and will try again.
Thanks
Allain
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!