Skip to content

Instantly share code, notes, and snippets.

@rirel
Last active November 18, 2015 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rirel/5a94724b29eb2fe1ba55 to your computer and use it in GitHub Desktop.
Save rirel/5a94724b29eb2fe1ba55 to your computer and use it in GitHub Desktop.
require 'socket'
socket = UDPSocket.open
socket.bind(Socket::IPADDR_ANY, 7090)
# what I have:
msg, host = socket.recvfrom(16)
# since host is an array, I can do this:
info, port, addr = host # last element is dropped
## Question #1:
# - Is there a way to make the assignments on lines 7 and 10 in one go?
#msg, [info, port, addr] = host # something along these lines?
## Question #2:
# - If I don't care about the addrinfo AF_INET thing, can I just not assign it?
# In Haskell you can use an underscore to capture from a list and discard
# values you don't need - is there a Ruby analogue for this?
x, y = %w{a b c} # c is dropped
#_, y, z = %w{a b c} # I want something like this where a is dropped
# IRB Session:
head :001 > _, y, z = %w{a b c}
=> ["a", "b", "c"]
head :002 > _
=> ["a", "b", "c"]
head :003 > y
=> "b"
head :004 > z
=> "c"
head :005 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment