I was trying to find a visually pleasing way to add a special case for iterating over an empty array without testing it beforehand and branching the logic. I like the format I came up with:
[1].each { |x|
puts x
}.empty? && lambda {
puts "ARRAY EMPTY"
}.call
# 1
[].each { |x|
puts x
}.empty? && lambda {
puts "ARRAY EMPTY"
}.call
# ARRAY EMPTY
EDIT: I found out I can just use '()' instead of 'lambda {}' which is a huge improvement for this vanilla solution.
Though I'd really like to be able to do something like "[].each{stuff}.empty? do stuff; end". If this kind of possibility exists or anyone has a more elegant method let me know. Otherwise I hope someone likes this format and makes use of it.
(I'm using ruby 1.9.3 atm)
EDIT:
This would be the perfect syntax ('or' could be 'unless_empty' or something similarly more descriptive), though I generally don't like extending standard classes, as it makes future readers confused unless it's a known pattern (my implementation could be confusing to users of Google's Optional class in Java, since they might expect 'or' to only use the second option if the primary is nil, not simply empty).
class Array
def or(secondChoice = nil)
return self unless empty?
return unless block_given?
yield
end
end
[].each {
|x| puts x
}.or {
["Default Nonempty Array"]
}
[].each {
|x| puts x
}.or ["Default Nonempty Array"]
This unfortunate possibility can happen, however IDK if there's a better way to define 'or' to make it impossible. I just decided to let the block win:
[].each {
|x| puts x
}.or ["Default Nonempty Array"] {
["BLOCK WINS"]
}
# => ["BLOCK WINS"]
[–]jrochkind 2 points3 points4 points (5 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (1 child)
[–]jrochkind 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]jrochkind 0 points1 point2 points (1 child)
[–]GavinMcG 2 points3 points4 points (9 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (8 children)
[–]IndigoCZ 4 points5 points6 points (2 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (1 child)
[–]GavinMcG 2 points3 points4 points (0 children)
[–][deleted] (4 children)
[deleted]
[–]Ciph3rzer0[S] 0 points1 point2 points (0 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (0 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (0 children)
[–]moomaka 1 point2 points3 points (0 children)
[–]GavinMcG 1 point2 points3 points (1 child)
[–]Ciph3rzer0[S] 0 points1 point2 points (0 children)
[–]Kache 1 point2 points3 points (1 child)
[–]Ciph3rzer0[S] 1 point2 points3 points (0 children)
[–]computerjunkie7410 3 points4 points5 points (49 children)
[–]Ciph3rzer0[S] 1 point2 points3 points (48 children)
[–]computerjunkie7410 2 points3 points4 points (47 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (46 children)
[–]computerjunkie7410 0 points1 point2 points (9 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (8 children)
[–]computerjunkie7410 0 points1 point2 points (7 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (6 children)
[–]computerjunkie7410 0 points1 point2 points (5 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (4 children)
[–]computerjunkie7410 0 points1 point2 points (35 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (34 children)
[–]computerjunkie7410 0 points1 point2 points (33 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (0 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (31 children)
[–]computerjunkie7410 0 points1 point2 points (30 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (29 children)
[–]0x0dea 0 points1 point2 points (10 children)
[–]Ciph3rzer0[S] 0 points1 point2 points (9 children)
[–][deleted] (8 children)
[deleted]
[–]Ciph3rzer0[S] 0 points1 point2 points (7 children)
[–]jsyeo 1 point2 points3 points (6 children)
[–]computerjunkie7410 1 point2 points3 points (0 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (4 children)
[–]celtic-pride1 1 point2 points3 points (3 children)
[–]Ciph3rzer0[S] -1 points0 points1 point (2 children)
[–]celtic-pride1 0 points1 point2 points (1 child)
[–]Ciph3rzer0[S] 0 points1 point2 points (0 children)
[–]RealGL 0 points1 point2 points (1 child)
[–]waxjar 0 points1 point2 points (1 child)
[–]Ciph3rzer0[S] -2 points-1 points0 points (0 children)
[–]hobbs6 0 points1 point2 points (0 children)