Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Created March 6, 2013 19:14
Show Gist options
  • Select an option

  • Save rsutphin/5102133 to your computer and use it in GitHub Desktop.

Select an option

Save rsutphin/5102133 to your computer and use it in GitHub Desktop.
Surprising each_with_object behavior
1.9.3p194 :001 > %w(a b c).each_with_object(['foo']) { nil }
=> ["foo"]
1.9.3p194 :002 > %w(a b c).each_with_object(['foo']) { break }
=> nil
@ordinaryzelig

Copy link
Copy Markdown

In case anybody like me is looking for a solution to break and still return the object mid-iteration. This works:

%w(a b c).each_with_object(['foo']) { |letter, array| break array }
#=> ['foo']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment