-
-
Save rirel/5a94724b29eb2fe1ba55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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