Created
November 9, 2012 10:50
-
-
Save sivagao/4045114 to your computer and use it in GitHub Desktop.
ruby best practices. four kind arguments[ordinal, optional arugment, pseudo-keyword argument, arguments as an Array] and block
This file contains hidden or 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
| # Standard ordinal arguments | |
| def distance(x1,y1,x2,y2) | |
| Math.hypot(x2 - x1, y2 - y1) | |
| end | |
| # Ordinal arguments, with an optional argument | |
| def load_file(name,mode="rb") | |
| File.open(name,mode) | |
| end | |
| # Pseudo-keyword arguments | |
| def story(options) | |
| "#{options[:person]} went to town, riding on a #{options[:animal]}" | |
| end | |
| # Treating arguments as an Array | |
| def distance2(*points) | |
| distance(*points.flatten) | |
| end | |
| >> distance(3,3,4,5) | |
| => 2.23606797749979 | |
| >> load_file "foo.jpg" | |
| => #<File:foo.jpg> | |
| >> load_file "foo.jpg", "r" | |
| => #<File:foo.jpg> | |
| >> load_file "foo.jpg", "kitten" | |
| ArgumentError: illegal access mode kitten ... | |
| >> story(animal: "Tiger", person: "Yankee Doodle") | |
| => "Yankee Doodle went to town, riding on a Tiger" | |
| >> story(person: "Yankee Doodle", animal: "Tiger") | |
| => "Yankee Doodle went to town, riding on a Tiger" | |
| >> distance2 [3,3], [4,5] | |
| => 2.23606797749979 | |
| def distance3(*points) | |
| case(points.length) | |
| when 2 | |
| x1,y1,x2,y2 = points.flatten | |
| when 4 | |
| x1,y1,x2,y2 = points | |
| else | |
| raise ArgumentError, | |
| "Points may be specified as [x1,y1], [x2,y2] or x1,y1,x2,y2" | |
| end | |
| Math.hypot(x2 - x1, y2 - y1) | |
| end | |
| def write_story_to_file(file,options={}) | |
| File.open(file,"w") { |f| f << story2(options) } | |
| end | |
| def story2(options={}) | |
| options = { person: "Yankee Doodle", animal: "Tiger" }.merge(options) | |
| "#{options[:person]} went to town, riding on a #{options[:animal]}" | |
| end | |
| require "socket" | |
| class Client | |
| def initialize(ip="127.0.0.1",port=3333) | |
| @ip, @port = ip, port | |
| end | |
| def send_message(msg) | |
| socket = TCPSocket.new(@ip,@port) | |
| socket.puts(msg) | |
| response = socket.gets | |
| ensure | |
| socket.close | |
| end | |
| def receive_message | |
| socket = TCPSocket.new(@ip,@port) | |
| response = socket.gets | |
| ensure | |
| socket.close | |
| end | |
| end | |
| require "socket" | |
| class Client | |
| def initialize(ip="127.0.0.1",port=3333) | |
| @ip, @port = ip, port | |
| end | |
| def send_message(msg) | |
| connection do |socket| | |
| socket.puts(msg) | |
| socket.gets | |
| end | |
| end | |
| def receive_message | |
| connection { |socket| socket.gets } | |
| end | |
| private | |
| def connection | |
| socket = TCPSocket.new(@ip,@port) | |
| yield(socket) | |
| ensure | |
| socket.close | |
| end | |
| end | |
| server = Server.new | |
| server.handle(/hello/i) { "Hello from server at #{Time.now}" } | |
| server.handle(/goodbye/i) { "Goodbye from server at #{Time.now}" } | |
| server.handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } | |
| server.run | |
| class Server | |
| def initialize(port=3333) | |
| @server = TCPServer.new('127.0.0.1',port) | |
| @handlers = {} | |
| end | |
| def handle(pattern, &block) | |
| @handlers[pattern] = block | |
| end | |
| def run | |
| while session = @server.accept | |
| msg = session.gets | |
| match = nil | |
| @handlers.each do |pattern,block| | |
| if match = msg.match(pattern) | |
| break session.puts(block.call(match)) | |
| end | |
| end | |
| unless match | |
| session.puts "Server received unknown message: #{msg}" | |
| end | |
| end | |
| end | |
| end | |
| class Server | |
| # other methods same as before | |
| def self.run(port=3333,&block) | |
| server = Server.new(port) | |
| server.instance_eval(&block) | |
| server.run | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment