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

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

Subject: Any ideas why IRB would be different than running the code?
Date: Sat 12 Jan 13 06:24:34AM +0900

Quoting Wayne B. ([email protected]):

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs