Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add method_missing

  • Loading branch information...
commit 143092b4a9857671c296cefdac7e697831d0f556 1 parent 83adb65
@phoet authored
View
6 .gems
@@ -0,0 +1,6 @@
+autotest (4.3.2)
+autotest-growl (0.2.4)
+mocha (0.9.8)
+rake (0.8.7)
+rdoc (2.5.8)
+redgreen (1.2.2)
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm use 1.8.7@which_ruby
View
12 examples/ex.rb
@@ -0,0 +1,12 @@
+$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+
+require 'which_ruby'
+
+include WhichRuby
+
+%w{jruby ruby ree rubinius}.each do |ru|
+ puts "checking #{ru}?"
+ check = send(:"#{ru}?")
+ puts " #{ru} is #{check}"
+ puts " with version #{version}" if check
+end
View
50 lib/which_ruby.rb
@@ -1,55 +1,79 @@
module WhichRuby
-# TODO add some name mapping rbx vs rubinius f.e.
+ @@mappings = {
+ :rbx => :rubinious,
+ }
+
+ @@rubies = [:ruby, :jruby, :rubinius, :ree]
def ruby_type
- return :"#{engine.downcase}" unless engine.nil?
- description.match(/\A([^\s]+)/)
+ return :"#{ruby_engine.downcase}" unless ruby_engine.nil?
+ ruby_description.match(/\A([^\s]+)/)
:"#{$1.downcase}"
end
alias_method :rt, :ruby_type
def is_ruby_type?(type)
- ruby_type == type
+ ruby_type == (@@mappings[type] || type)
end
alias_method :r?, :is_ruby_type?
-
- def version
+
+ def ruby_version
RUBY_VERSION
end
- def description
+ def ruby_description
RUBY_DESCRIPTION
end
- def copyright
+ def ruby_copyright
RUBY_COPYRIGHT
end
- def patchlevel
+ def ruby_patchlevel
RUBY_PATCHLEVEL
end
- def platform
+ def ruby_platform
RUBY_PLATFORM
end
- def release_date
+ def ruby_release_date
RUBY_RELEASE_DATE
end
- def revision
+ def ruby_revision
RUBY_REVISION
rescue
nil
end
- def engine
+ def ruby_engine
RUBY_ENGINE
rescue
nil
end
+ def respond_to?(sym)
+ contains_ruby_type?(strip_sym(sym)) || super(sym)
+ end
+
+ def method_missing(sym, *args, &block)
+ name = strip_sym(sym)
+ return is_ruby_type?(name) if contains_ruby_type?(name)
+ super(sym, *args, &block)
+ end
+
+ private
+
+ def strip_sym(sym)
+ sym.to_s.downcase[0..-2].to_sym
+ end
+
+ def contains_ruby_type?(name)
+ @@rubies.include? name
+ end
+
end
View
31 readme.textile
@@ -1,28 +1,45 @@
h2. About
-*which_ruby* is a simple helper for checking running Ruby engine
+*which_ruby* is a simple helper for checking running Ruby engine.
h2. Usage
+include WhichRuby:
<pre>
-rvm use 1.8.7
+rvm use jruby
gem install which_ruby
irb
> require 'which_ruby'
=> true
> include WhichRuby
-=> Object
+=> Object
+</pre>
+
+access the API:
+<pre>
> ruby_type
-=> :ruby
- > is_ruby_type? :ruby
+=> :jruby
+ > rt
+=> :jruby
+ > is_ruby_type? :jruby
=> true
- > r? :ruby and rt == :ruby
+ > r? :jruby
+=> true
+ > jruby?
=> true
</pre>
+h2. Examples
+
+Run the examples with different runtimes, jruby for example:
+
+<pre>
+rvm use jruby
+ruby examples/ex.rb
+</pre>
+
h2. TODO
* improve me!
* more docs
-* sensible symbol naming mapping
* more todos
View
17 test/test_which_ruby.rb
@@ -8,15 +8,15 @@ def setup
end
def test_r_type_from_engine
- @helper.stubs(:engine).returns("jruby")
+ @helper.stubs(:ruby_engine).returns("jruby")
assert_equal(:jruby, @helper.ruby_type)
assert_equal(:jruby, @helper.rt)
end
def test_r_type_from_description
- @helper.stubs(:engine).returns(nil)
- @helper.stubs(:description).returns("ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.2.0]")
+ @helper.stubs(:ruby_engine).returns(nil)
+ @helper.stubs(:ruby_description).returns("ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.2.0]")
assert_equal(:ruby, @helper.ruby_type)
end
@@ -29,4 +29,15 @@ def test_which_r?
assert_equal(true, @helper.is_ruby_type?(:ruby))
assert_equal(true, @helper.r?(:ruby))
end
+
+ def test_respond_to?
+ @helper.stubs(:ruby_engine).returns("jruby")
+ assert_equal(true, @helper.respond_to?(:jruby?))
+ end
+
+ def test_jruby?
+ @helper.stubs(:ruby_engine).returns("jruby")
+ assert_equal(true, @helper.jruby?)
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.