Skip to content

Instantly share code, notes, and snippets.

@stevecj
Last active December 2, 2019 06:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevecj/9ace6a70370f6d1a1511 to your computer and use it in GitHub Desktop.
Save stevecj/9ace6a70370f6d1a1511 to your computer and use it in GitHub Desktop.
Ruby will destructure objects that are not arrays, but respond to #to_ary
S=Struct.new(:a,:b)
ss = [S.new(1,2), S.new(3,4)]
p ss.map{|a,b| "#{a} #{b}" }
# => ["#<struct S a=1, b=2> ", "#<struct S a=3, b=4> "]
class S ; def to_ary ; to_a ; end ; end
p ss.map{|a,b| "#{a} #{b}" }
# => ["1 2", "3 4"]
@guizmaii
Copy link

guizmaii commented Dec 6, 2016

Great trick ! Thanks :) I talked about your trick here : http://stackoverflow.com/a/40998675/2431728 ;)

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