Skip to content

Commit

Permalink
🐛 Attempt a fix of Ruby's String#b collision.
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Mar 2, 2015
1 parent b2e9e2e commit fd54bf6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 8 additions & 5 deletions .travis.yml
Expand Up @@ -2,11 +2,14 @@
language: ruby
script: "bundle exec qed"
rvm:
- 1.8.7
- 1.9.2
- 2.1.0
- 1.9.3
- 1.9.2
- 1.8.7
- rbx
- rbx-19mode
- jruby
- jruby-19mode

matrix:
allow_failures:
- rvm: rbx
- rvm: rbx-2
cache: bundler
15 changes: 14 additions & 1 deletion lib/radix/operator.rb
Expand Up @@ -40,6 +40,17 @@ def b(base)
# instances.
class ::String

# Ruby 2.x defines it's own `String#b` method for converting to ASCII 8-bit.
# That breaks Radix (of course), but it a terrbile name. `String#ascii` is
# much better (duh!). So that's what we are doing.
if method_defined?(:b)
alias :ascii :b
else
def ascii
force_encoding('ASCII')
end
end

##
# Takes a String and makes it into a Radix::Integer or Radix::Float as given
# base. Float is determined by a "." character in string instance
Expand All @@ -48,7 +59,9 @@ class ::String
# The desired base.
#
# @return [Radix::Integer, Radix::Float]
def b(base)
def b(base=nil)
return ascii unless base

if index('.')
Radix::Float.new(self, base)
else
Expand Down

0 comments on commit fd54bf6

Please sign in to comment.