ChillerDragon / teeworlds_network

A teeworlds 0.7 network protocol library written in ruby
5 stars 0 forks source link

Using ``return`` in the block exists the client #1

Closed ChillerDragon closed 2 years ago

ChillerDragon commented 2 years ago
require_relative 'lib/teeworlds-client'

client = TeeworldsClient.new(verbose: false)

client.on_chat do |msg|
  return if msg.message[0] != '!'

  case msg.message[1..]
  when 'ping' then client.send_chat('pong')
  when 'whoami' then client.send_chat("You are: #{msg.author.name}")
  when 'list' then client.send_chat(client.game_client.players.values.map(&:name).join(', '))
  else client.send_chat('Unkown command! Commands: !ping, !whoami, !list')
  end
end
ChillerDragon commented 2 years ago

Do you even ruby bro!? Here read this article https://medium.com/swlh/returning-from-a-ruby-proc-beware-of-where-you-land-a29add7ddd9d

tl;dr

return in blocks returns in the scope where the block is created not inside of the block. so use next instead