Difference between revisions of "Ruby Notes"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 3: | Line 3: | ||
* http://www.ruby-lang.org/en/documentation/ | * http://www.ruby-lang.org/en/documentation/ | ||
* [http://www.ruby-lang.org/en/documentation/quickstart/ Quick Start] | * [http://www.ruby-lang.org/en/documentation/quickstart/ Quick Start] | ||
* [http://en.wikibooks.org/wiki/Ruby_Programming WikiBooks.org Ruby manual] | |||
=Class vs. Instance Variables= | =Class vs. Instance Variables= | ||
* http://www.rubycentral.com/pickaxe/tut_classes.html | * http://www.rubycentral.com/pickaxe/tut_classes.html | ||
* http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes | |||
* http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby | * http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby | ||
* http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby | * http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby |
Revision as of 10:44, 23 September 2009
References
Class vs. Instance Variables
- http://www.rubycentral.com/pickaxe/tut_classes.html
- http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes
- http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby
- http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby
- http://martinfowler.com/bliki/ClassInstanceVariable.html
The definition of the class instance variable is the fragment
class << self; attr_accessor :instances; end.
This defines an instance variable (and getters and setters) on the class that's inherited by its descendents. Unlike class variables these class instance variables will take different values for each class object.
HTTP
Refactor
module Anubis module ShardAccess require "net/http" LIB_VERSION = 'r1b' class Read attr_accessor :path, :body, :headers def initialize( path_in, body_in, accept='xml', limit=10 ) self.path = path_in self.body = body_in self.headers = { 'Interface' => 'S1', 'Accept' => accept, 'Limit' => limit.to_s, 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'RB_SHARD_ACCESS_LIB_' + Anubis::ShardAccess::LIB_VERSION } end def call ( host, port ) http = Net::HTTP.new( host, port ) resp = http.post2( @path, @body, @headers ) end end end end host = 'IP.AD.RE.SS' port = 10001 path = '/message' body = "query_string" req = Anubis::ShardAccess::Read.new( path, body, 'csv', 1 ) data = req.call( host, port ) puts data.body
to
# no need to put this within the module body as it will be loaded regardless require 'net/http' module Anubis module ShardAccess LIB_VERSION = 'r1b' class Read attr_accessor :path, :body, :headers #usually past a few args you should provide an options has to make things more readable # such as Read.new foo, bar, :accept => :csv, :limit => 1 def initialize path, body, options = {} @path, @body = path, body @headers = { 'Interface' => 'S1', 'Accept' => options.fetch(:accept, :xml), # Hash#fetch will use the keys value when found, otherwise the second argument is used (:xml) 'Limit' => options.fetch(:limit, 10), 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'RB_SHARD_ACCESS_LIB_' + LIB_VERSION } end def call host, port Net::HTTP.new(host, port).post2 path, body, headers end end end end