Skip to content

Instantly share code, notes, and snippets.

@yoggy
Last active July 11, 2016 07:51
Show Gist options
  • Save yoggy/955d93cc8d012fd1b38955eb397f00bb to your computer and use it in GitHub Desktop.
Save yoggy/955d93cc8d012fd1b38955eb397f00bb to your computer and use it in GitHub Desktop.
require 'mathn'するとruby-mqttが動かない例

require 'mathn'するとruby-mqttが動かない例

$ 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-test1.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

$ ruby mqtt-test1.rb  ← これは正しく実行される

$ diff -u mqtt-test1.rb mqtt-test2.rb
--- mqtt-test1.rb       2016-07-11 10:51:58.236447209 +0900
+++ mqtt-test2.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-test2.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-test2.rb:11:in `<main>'
@yoggy
Copy link
Author

yoggy commented Jul 11, 2016

ruby-mqttを修正するのがいちばん手っ取り早いかもしれないけど、mathnライブラリのデザインの問題のような気も。

何かライブラリを作る時に、require 'mathn'されたときを想定して注意しながらコーディングしないとダメなのはちょっと微妙…

@nacl-ando
Copy link

mqttの/lib/mqtt/packet.rb:199で書かれてる digit |= 0x80 if (body_length > 0)
で、digitが期待する値ってなんだろう、と思いました。
これ、元々のdigitがIntegerで、body_length > 0の場合はtrueが入っちゃうようです。
もしもbody_length >0のときに0x80がはいって欲しい場合は digit ||=0x80 if (body_length > 0)が文法的に正しそう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment