0

I'm trying to fix a bug in a third party application (vagrant). I have 2 variables and for some reason they are not equal. I've checked and they are both strings and both have the same value. What can be the reason?

Debug string:

"'" + Vagrant::VERSION + "', '" + windows_version + "', " + Vagrant::VERSION.class.name + ", " + windows_version.class.name + ", " + (windows_version == Vagrant::VERSION ? "true" : "false")

Output:

'1.9.6', '1.9.6', String, String, false
Poma
  • 8,174
  • 18
  • 82
  • 144
  • 1
    Possible duplicate of [What's the difference between equal?, eql?, ===, and ==?](https://stackoverflow.com/questions/7156955/whats-the-difference-between-equal-eql-and) – Bill the Lizard Aug 18 '17 at 13:55
  • 3
    Print your debugging with this code: `p Vagrant::VERSION; p windows_version`. That's easier than what you've got, but more importantly, it will show differences that may be otherwise hidden. – Wayne Conrad Aug 18 '17 at 14:00
  • This is not a duplicate of the nominated exemplar. The OP is using `==` to compare strings, which is the normal way to do it in Ruby. `==` is not sensitive to object identity; it only cares about the object's value, so it's the right way for the OP to do it. – Wayne Conrad Aug 18 '17 at 14:02
  • 1
    You are correct! `p windows_version` outputs `"1.9.6\e[0m"`. Now what does this mean and how do I trim it? – Poma Aug 18 '17 at 14:08
  • How did you get / where was the `windows_version` variable defined? It would be better to fix the source of the problem than to retro-fit a fix by trimming it. It looks like you've somehow got an ASCII escape sequence *in the version number* (?!) – Tom Lord Aug 18 '17 at 15:34
  • The bug is definitely not in vagrant – Mark Thomas Aug 18 '17 at 15:36
  • 1
    `windows_version` is initialized by vagrant in [platform.rb:398](https://github.com/mitchellh/vagrant/blob/master/lib/vagrant/util/platform.rb#L398) by running vagrant.exe, getting its stdout and matching it with regex. [Relevant issue on GitHub](https://github.com/mitchellh/vagrant/issues/8901) – Poma Aug 18 '17 at 15:42
  • The bug definitely might be in vagrant :D – Tom Lord Aug 18 '17 at 15:44
  • Well as a quick hack-fix you could use: `windows_version[/[\d.]+/]` to strip it? – Tom Lord Aug 18 '17 at 15:46
  • That's what I did on my installation, but for a pull request more general approach is needed. I've tried removing it with `.gsub(/[^[:print:]]/,'')` but it only removes `\e`. Matching it with regex feels less general but looks like `[\w.-]` will be enough for vagrant versioning scheme. – Poma Aug 18 '17 at 15:54
  • Created a [Pull Request](https://github.com/mitchellh/vagrant/pull/8902) for vagrant. – Poma Aug 18 '17 at 16:07
  • 1
    `[ .... ].map(&:inspect).join(',')` is probably a better way of getting this sort of string. What you have here is borderline baffling. – tadman Aug 18 '17 at 16:18
  • @tadman Just `.inspect` is simpler: It works fine on arrays. And, if you're able to use `p`, that calls #inspect for you. – Wayne Conrad Aug 18 '17 at 17:35
  • @WayneConrad If that format is fine, absolutely. The `map` call is just a step towards having more control over the whole process. – tadman Aug 18 '17 at 17:35

0 Answers0