Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Defining force_encoding wrapper in WebSocket class instead of definin…

…g String#force_encoding in Ruby 1.8, to avoid conflict with e.g. json_pure gem.
  • Loading branch information...
commit 47821e8b179f722b82268093d85cc877dd961f35 1 parent 818bc07
@gimite authored
Showing with 10 additions and 37 deletions.
  1. +10 −37 lib/web_socket.rb
View
47 lib/web_socket.rb
@@ -6,41 +6,6 @@
require "uri"
-if RUBY_VERSION < "1.9.0"
-
- class Encoding
-
- def self.const_missing(name)
- return Encoding.new()
- end
-
- end
-
- class String
-
- def force_encoding(encoding)
- return self
- end
-
- def ord
- return self[0]
- end
-
- alias bytesize size
-
- end
-
- class Integer
-
- def ord
- return self
- end
-
- end
-
-end
-
-
class WebSocket
class << self
@@ -135,7 +100,7 @@ def send(data)
if !@handshaked
raise(WebSocket::Error, "call WebSocket\#handshake first")
end
- data = data.dup().force_encoding("ASCII-8BIT")
+ data = force_encoding(data.dup(), "ASCII-8BIT")
write("\x00#{data}\xff")
flush()
end
@@ -149,7 +114,7 @@ def receive()
if !(packet =~ /\A\x00(.*)\xff\z/nm)
raise(WebSocket::Error, "input must start with \\x00 and end with \\xff")
end
- return $1.force_encoding("UTF-8")
+ return force_encoding($1, "UTF-8")
end
def tcp_socket
@@ -211,6 +176,14 @@ def flush()
@socket.flush()
end
+ def force_encoding(str, encoding)
+ if str.respond_to?(:force_encoding)
+ return str.force_encoding(encoding)
+ else
+ return str
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.