Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow [] access to methods for greater backwards compatibility

  • Loading branch information...
commit d63d2e5a7d866b7036914fc73bb2689a83b4ac72 1 parent 3bbb15e
@sferik authored
Showing with 29 additions and 0 deletions.
  1. +4 −0 lib/twitter/base.rb
  2. +25 −0 spec/twitter/base_spec.rb
View
4 lib/twitter/base.rb
@@ -6,6 +6,10 @@ def initialize(hash={})
end
end
+ def [](method)
+ self.__send__(method.to_sym)
+ end
+
def to_hash
Hash[instance_variables.map{|ivar| [ivar[1..-1].to_sym, instance_variable_get(ivar)]}]
end
View
25 spec/twitter/base_spec.rb
@@ -0,0 +1,25 @@
+require 'helper'
+
+describe Twitter::Base do
+
+ before do
+ @base = Twitter::Base.new('id' => 1)
+ end
+
+ describe "#[]" do
+ it "should be able to call methods using [] with symbol" do
+ @base[:object_id].should be_an Integer
+ end
+ it "should be able to call methods using [] with string" do
+ @base['object_id'].should be_an Integer
+ end
+ end
+
+ describe "#to_hash" do
+ it "should return a hash" do
+ @base.to_hash.should be_a Hash
+ @base.to_hash[:id].should == 1
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.