Skip to main content
added 221 characters in body
Source Link
aenw
  • 851
  • 9
  • 19

Just to add to what steenslag said:

detect doesn't always return nil.

You can pass in a lambda to execute (call) if detect does not 'detect' (find) an item. In other words, you can tell detect what to do if it can't detect (find) something.

To add to your example: not_found = lambda { "uh oh. couldn't detect anything!"} @array.detect(not_found) {|h| h[:status] == 'X'} # try to find something that isn't in the Enumerable object

not_found = lambda { "uh oh. couldn't detect anything!"} # try to find something that isn't in the Enumerable object: @array.detect(not_found) {|h| h[:status] == 'X'} 

will return     "uh oh. couldn't detect anything!"  

This means that you don't have to write this kind of code: if @array.detect {|h| h[:status] == 'X'} isNil

if (result = @array.detect {|h| h[:status] == 'X'}).nil? # show some error, do something here to handle it # (this would be the behavior you'd put into your lambda) else # deal nicely with the result end 

That's one major difference between any? and detect -- you can't tell any? what to do if it doesn't find any items.

This is in the Enumerable class. ref: http://ruby-doc.org/core/classes/Enumerable.html#M003123

Just to add to what steenslag said:

detect doesn't always return nil.

You can pass in a lambda to execute (call) if detect does not 'detect' (find) an item. In other words, you can tell detect what to do if it can't detect (find) something.

To add to your example: not_found = lambda { "uh oh. couldn't detect anything!"} @array.detect(not_found) {|h| h[:status] == 'X'} # try to find something that isn't in the Enumerable object

will return    "uh oh. couldn't detect anything!"  

This means that you don't have to write this kind of code: if @array.detect {|h| h[:status] == 'X'} isNil

That's one major difference between any? and detect -- you can't tell any? what to do if it doesn't find any items.

This is in the Enumerable class. ref:

Just to add to what steenslag said:

detect doesn't always return nil.

You can pass in a lambda to execute (call) if detect does not 'detect' (find) an item. In other words, you can tell detect what to do if it can't detect (find) something.

To add to your example:

not_found = lambda { "uh oh. couldn't detect anything!"} # try to find something that isn't in the Enumerable object: @array.detect(not_found) {|h| h[:status] == 'X'} 

will return  "uh oh. couldn't detect anything!"

This means that you don't have to write this kind of code:

if (result = @array.detect {|h| h[:status] == 'X'}).nil? # show some error, do something here to handle it # (this would be the behavior you'd put into your lambda) else # deal nicely with the result end 

That's one major difference between any? and detect -- you can't tell any? what to do if it doesn't find any items.

This is in the Enumerable class. ref: http://ruby-doc.org/core/classes/Enumerable.html#M003123

Source Link
aenw
  • 851
  • 9
  • 19

Just to add to what steenslag said:

detect doesn't always return nil.

You can pass in a lambda to execute (call) if detect does not 'detect' (find) an item. In other words, you can tell detect what to do if it can't detect (find) something.

To add to your example: not_found = lambda { "uh oh. couldn't detect anything!"} @array.detect(not_found) {|h| h[:status] == 'X'} # try to find something that isn't in the Enumerable object

will return "uh oh. couldn't detect anything!"

This means that you don't have to write this kind of code: if @array.detect {|h| h[:status] == 'X'} isNil

That's one major difference between any? and detect -- you can't tell any? what to do if it doesn't find any items.

This is in the Enumerable class. ref: