Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yoggy/aa77faba0a0840ef0faf2182d23a665c to your computer and use it in GitHub Desktop.
Save yoggy/aa77faba0a0840ef0faf2182d23a665c to your computer and use it in GitHub Desktop.

Example of ruby-mqtt does not work with mathn library

$ ruby -v
ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]

$ gem install mqtt
$ gem list | grep mqtt
mqtt (0.4.0)

$ cat mqtt-test-ok.rb
#!/usr/bin/ruby

require 'mqtt'

conn_opts = {
    "remote_host" => "iot.eclipse.org",
    "remote_port" => 1883
}

MQTT::Client.connect(conn_opts) do |c|
  c.publish("test/topic/123", "message")
end

puts "success"

$ ruby mqtt-test-ok.rb
success

$ diff -u mqtt-test-ok.rb mqtt-test-ng.rb
--- mqtt-test-ok.rb     2016-07-11 10:51:58.236447209 +0900
+++ mqtt-test-ng.rb     2016-07-11 10:52:53.340455240 +0900
@@ -1,6 +1,7 @@
 #!/usr/bin/ruby

 require 'mqtt'
+require 'mathn'

 conn_opts = {
     "remote_host" => "iot.eclipse.org",


$ ruby mqtt-test-ng.rb
/var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/packet.rb:199:in `to_s': undefined method `|' for (17/64):Rational (NoMethodError)
        from /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/client.rb:545:in `block in send_packet'
        from /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/client.rb:544:in `synchronize'
        from /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/client.rb:544:in `send_packet'
        from /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/client.rb:287:in `connect'
        from /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/client.rb:93:in `connect'
        from mqtt-test-ng.rb:11:in `<main>'



$ vi /var/lib/gems/2.3.0/gems/mqtt-0.4.0/lib/mqtt/packet.rb
          .
          .
          .
194       # Build up the body length field bytes
195       begin
196         digit = (body_length % 128)
197         body_length = (body_length / 128)  #### Fixnum -> Rational ####
198         # if there are more digits to encode, set the top bit of this digit
199         digit |= 0x80 if (body_length > 0)
200         header.push(digit)
201       end while (body_length > 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment