9 Replies - 704 Views - Last Post: 28 January 2014 - 08:48 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

ruby method?

Posted 28 January 2014 - 02:59 PM

I am seeking out a method that iterates through an array and outputs the first and only the first element in that array that meets a conditional. I thought I saw this somewhere once but I am having trouble finding it. Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: ruby method?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,208
  • Joined: 21-June 11

Re: ruby method?

Posted 28 January 2014 - 03:07 PM

The method you're looking for is find from the Enumerable module.

This post has been edited by sepp2k: 28 January 2014 - 03:08 PM

Was This Post Helpful? 2
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: ruby method?

Posted 28 January 2014 - 03:08 PM

my_array.find{|x| x.some_test }

or
my_array.find(&:some_test)

I'll have to test this though as it doesn't appear in the docs.. strange?!
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: ruby method?

Posted 28 January 2014 - 03:13 PM

Mm it appears under Enumerable.find, but doesn't use find, uses detect:

(1..100).detect {|i| i % 5 == 0 and i % 7 == 0 }   #=> 35

Ruby is a little odd :dontgetit:



Anyway, find works:
my_array = [1,2,3,4,5]

y = my_array.find{|x| x.even? }
puts y    # 2

This post has been edited by andrewsw: 28 January 2014 - 03:16 PM

Was This Post Helpful? 1
  • +
  • -

#5 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: ruby method?

Posted 28 January 2014 - 03:20 PM

That's exactly what is was looking for!!! Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,208
  • Joined: 21-June 11

Re: ruby method?

Posted 28 January 2014 - 03:28 PM

View Postandrewsw, on 28 January 2014 - 11:08 PM, said:

it doesn't appear in the docs.. strange?!


Right, the docs for a class don't list inherited methods, which is a bit unfortunate.

View Postandrewsw, on 28 January 2014 - 11:13 PM, said:

Mm it appears under Enumerable.find, but doesn't use find, uses detect


That's because aliases don't get their own docstring - they just automatically get the same description as the aliased method.
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: ruby method?

Posted 28 January 2014 - 03:52 PM

Thanks @sepp2k

Still odd though that the page heading/title is for "find" but it doesn't mention this method in the actual page content. Must be very confusing :dozingoff:
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,208
  • Joined: 21-June 11

Re: ruby method?

Posted 28 January 2014 - 04:44 PM

Yeah, they really should list all the aliases of a method in the docs for that method.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: ruby method?

Posted 28 January 2014 - 05:16 PM

View Postsepp2k, on 28 January 2014 - 11:44 PM, said:

Yeah, they really should list all the aliases of a method in the docs for that method.

Yup. I shall speak to Mr Ruby ;)
Was This Post Helpful? 0
  • +
  • -

#10 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,425
  • Joined: 28-November 09

Re: ruby method?

Posted 28 January 2014 - 08:48 PM

If you want to get good with Ruby, read through Enumerable's docs multiple times for a month.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1