cmd, args = search_commands(some_string)
str = "say to 1=* in 2=* 3=*"
m = str.scan(/(\d+=\*)/i)
SAY TO OLOGO IN ELVISH HELLO THERE, HOW ARE YOU?
is equal to…
SAY TO 1=* IN 2=* 3=*
1=Ologo
2=Elvish
3=Hello there, how are you?
#!/usr/bin/ruby -w
# chat.rb
require 'gserver'
class ChatServer < GServer
def initialize(port=4000, host=GServer::DEFAULT_HOST)
@clients = []
super(port, host, Float::MAX, $stderr, true)
end
def serve(sock)
begin
@clients << sock
hostname = sock.peeraddr[2] || sock.peeraddr[3]
@clients.each do |c|
c.puts "#{hostname} has joined the chat." unless c == sock
end
until sock.eof? do
message = sock.gets.chomp
break if message == "/quit"
@clients.each { |c| c.puts "#{hostname}: #{message}" unless c == sock }
end
ensure
@clients.delete(sock)
@clients.each { |c| c.puts "#{hostname} has left the chat." }
end
end
end
server = ChatServer.new(*ARGV[0..2] || 4000)
server.start(-1)
while true do
sleep 2
puts "tick"
end
server.join
@clients.each { |c| c.puts "#{hostname}: #{message}" unless c == sock }is more perl-like. :)
I'm just looking for a little bit of advice. I'm relatively new to Ruby and trying to get to grips with the language as best as possible. I've been doing good up until now and now I'm stumped. Besides the fact that I have never did anything like this before, it is even worse trying to do it in a language that is relatively new to you.
I'm looking to create a verb-like system for commands. Using formats applied to commands I should be able to define a format like so:
Like it says, it takes the arguments passed to a search function and compares it to the format to see if it is true. 1=% and 2=% being the arguments that are passed. The function will then pass back the command to be executed and the arguments to be passed where 1=% is equal to 'Ologo' for instance. Obviously the arguments would be an array.
So any ideas would be great! Thank you.