Forum: Ruby Any ideas why IRB would be different than running the code?

Posted by Wayne Brisette (Guest)
on 2013-01-11 22:24
(Received via mailing list)
I have a routine I use for converting hex, decimal, and binary values 
between
each.

1.9.3-p362 :001 > class String
1.9.3-p362 :002?>     def convert_base(from, to)
1.9.3-p362 :003?>         self.to_i(from).to_s(to)
1.9.3-p362 :004?>       end
1.9.3-p362 :005?>   end
 => nil

1.9.3-p362 :017 > ttext = 10
 => 10
1.9.3-p362 :018 > ttext.to_s.convert_base(2, 16)
 => "2"

So this binary value (10) produces 2, the correct hex value.

However, when I use the same exact code in my script, I'm not seeing 2, 
I'm
seeing 10.

I used RubyMine to step through the code and I see it return 10 from
convert_base.

Any ideas why IRB would differ? Is there something wrong in my code?

Wayne
Posted by Carlo E. Prelz (Guest)
on 2013-01-12 07:16
(Received via mailing list)
Subject: Any ideas why IRB would be different than running the code?
  Date: Sat 12 Jan 13 06:24:34AM +0900

Quoting Wayne Brisette (wbrisett@att.net):

> Any ideas why IRB would differ? Is there something wrong in my code?

Must be something different in the script you use. If I execute this 
script:

--8<----8<----8<----8<----8<----8<----8<----8<--
class String
  def convert_base(from,to)
    self.to_i(from).to_s(to)
  end
end

ttext=10
puts("Result: #{ttext.to_s.convert_base(2,16)}")
--8<----8<----8<----8<----8<----8<----8<----8<--

I obtain

Result: 2

Carlo
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.