Skip to content

Instantly share code, notes, and snippets.

@6ewis
Last active January 3, 2016 18:59
Show Gist options
  • Save 6ewis/8a22b91758e06511b666 to your computer and use it in GitHub Desktop.
Save 6ewis/8a22b91758e06511b666 to your computer and use it in GitHub Desktop.
block and arguments decomposition
puts a.uniq.
map { | e | [a.count(e), e] }.
select { | c, _ | c > 1 }.
sort.reverse.
map { | c, e | "#{e}:#{c}" }
09:51 Lewix: >> [[1, "a"], [1, "d"], [3, "c"], [2, "b"]].select { |c, _| c > 1}
09:51 eval-in: Lewix => [[3, "c"], [2, "b"]] (https://eval.in/91745)
09:51 yatish27 has left IRC (Remote host closed the connection)
09:51 yatish27 has joined (~yatish27@123.201.38.248)
09:51 hopkins83: Hey Lewix :)
09:51 MrZYX: that's two parameters in the block passed to select
09:52 MrZYX: and what you see here is array decomposition at work
09:52 Lewix: MrZYX: sorry that's what I meant
09:52 MrZYX: it's basically the same as a, b = ["a", "b"]
09:52 Lewix: hopkins83: hello there
09:52 hopkins83: Lewix: Thanks for last time!
09:52 hopkins83: Lewix: I remember you helped me with some stuff a while back.
09:52 MrZYX: select iterates over the outer array, passing each inner array to the block
09:53 MrZYX: that decomposes the two element arrays, writing the first element into c and ignoring the second one (calling a variable _ says I don't care about it)
09:54 r0bgleeson has joined (~robert@subtle/contributor/robgleeson)
09:54 MrZYX: so it's a feature of how blocks work, not how select works
09:54 Lewix: MrZYX: beautiful explanation
09:54 Lewix: hopkins83: anytime
09:55 yfeldblum has left IRC (Ping timeout: 248 seconds)
09:55 Lewix: MrZYX: thanks
09:55 MrZYX: yw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment