#293 new
oleg dashevskii

[BUG] Non-working code snippet

Reported by oleg dashevskii | January 30th, 2008 @ 12:15 PM | in 1.0 preview

While testing #291 I created a small code snippet to assist myself. And it triggered some bug in Rubinius.

This DOESN'T WORK:

def hexputs(x)
  puts (0...x.length).map { |i| sprintf("%02x", x[i]) }.join(' ')
end

hexputs("abc")
~/work/rubinius% shotgun/rubinius y1.rb
0...3
An exception has occurred:
    No method 'map' on an instance of NilClass. (NoMethodError)

Backtrace:
    Kernel(NilClass)#map (method_missing_cv) at kernel/core/kernel.rb:540
                              Object#hexputs at y1.rb:2
                             main.__script__ at y1.rb:5
                    CompiledMethod#as_script at kernel/core/compiled_method.rb:206
                         Compile.single_load at kernel/core/compile.rb:219
                 Compile.load_from_extension at kernel/core/compile.rb:291
                             main.__script__ at kernel/loader.rb:176

But if I change code a bit (introduce variable 's'), it WORKS:

def hexputs(x)
  s = (0...x.length).map { |i| sprintf("%02x", x[i]) }.join(' ')
  puts s
end

hexputs("abc")

What's more interesting, after getting rid of function it also works:

x = "abc"
puts (0...x.length).map { |i| sprintf("%02x", x[i]) }.join(' ')

Another way to get first snippet working is to enclose puts argument in parentheses:

def hexputs(x)
  puts ((0...x.length).map { |i| sprintf("%02x", x[i]) }.join(' '))
end

hexputs("abc")

Needless to say, MRI is OK with all the cases.

I wish I could write a spec for that.

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tags