Skip to content

Commit fd54bf6

Browse files
committed
🐛 Attempt a fix of Ruby's String#b collision.
1 parent b2e9e2e commit fd54bf6

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

.travis.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
language: ruby
33
script: "bundle exec qed"
44
rvm:
5-
- 1.8.7
6-
- 1.9.2
5+
- 2.1.0
76
- 1.9.3
7+
- 1.9.2
8+
- 1.8.7
89
- rbx
9-
- rbx-19mode
1010
- jruby
11-
- jruby-19mode
12-
11+
matrix:
12+
allow_failures:
13+
- rvm: rbx
14+
- rvm: rbx-2
15+
cache: bundler

lib/radix/operator.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def b(base)
4040
# instances.
4141
class ::String
4242

43+
# Ruby 2.x defines it's own `String#b` method for converting to ASCII 8-bit.
44+
# That breaks Radix (of course), but it a terrbile name. `String#ascii` is
45+
# much better (duh!). So that's what we are doing.
46+
if method_defined?(:b)
47+
alias :ascii :b
48+
else
49+
def ascii
50+
force_encoding('ASCII')
51+
end
52+
end
53+
4354
##
4455
# Takes a String and makes it into a Radix::Integer or Radix::Float as given
4556
# base. Float is determined by a "." character in string instance
@@ -48,7 +59,9 @@ class ::String
4859
# The desired base.
4960
#
5061
# @return [Radix::Integer, Radix::Float]
51-
def b(base)
62+
def b(base=nil)
63+
return ascii unless base
64+
5265
if index('.')
5366
Radix::Float.new(self, base)
5467
else

0 commit comments

Comments
 (0)