Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel#proc with no arguments inside a method, fails with ArgumentError: tried to create Proc object without a block #3915

Closed
djellemah opened this issue May 23, 2016 · 5 comments
Milestone

Comments

@djellemah
Copy link

jruby 9.0.5.0 and 9.1.0.0

OS:
gentoo linux kernel 4.1.15, 64 bit kernel, 32-bit userland
gentoo linux kernel 4.1.15, 64 bit kernel, 64-bit userland

~ $ java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

def this_works( ary )
  if block_given?
    ary.map &Proc.new # this works
  end
end

this_works [9,8,7] do |item|
  item * 2
end

def this_doesnt_work( ary )
  if block_given?
    ary.map &proc # this *should* work, and does in MRI 2.1.10 .. 2.3.1
  end
end

this_doesnt_work [3,2,1] do |item|
  item * 2
end
@ekreutz
Copy link

ekreutz commented Jun 3, 2016

I have a similar issue, which I got narrowed down to the following

System info:
OS X El Capitan
10.11.3

java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)

jruby versions (both versions reproduce the issue)
jruby-1.7.23 [ x86_64 ]
jruby-9.1.2.0 [ x86_64 ]

Example code:

# this method somehow gets mangled in compilation?
def failingMethod
    ::Proc.new.call
end

# if this is used instead, everything compiles well!
def workingMethod
    Proc.new.call
end

some_var = "it fails!"

failingMethod do
    some_var = "it works!"
end

# should print "it works!"
puts some_var

When run, the above code produces the output:

ArgumentError: tried to create Proc object without a block
            new at org/jruby/RubyProc.java:130
  failingMethod at hello.rb:2
         (root) at hello.rb:7

If the same ruby file is run with compilation turned off, everything works fine.

@doudou
Copy link

doudou commented Jan 25, 2017

Still present in 9.1.7.0

@headius
Copy link
Member

headius commented Jan 25, 2017

This isn't something we're likely to fix.

It works in MRI only because of the way MRI is implemented. The current call's passed block just happens to be available when calling proc, and proc just happens to not care where the block comes from. Similar behavior happens for Proc.new, but both behaviors are discouraged and may go away in future versions of Ruby.

FWIW, Proc.new does currently work only because we make a special case for it in our compiler.

Your best bet short term is to either use Proc.new or capture the block in the method signature with &block.

@doudou
Copy link

doudou commented Jan 25, 2017

It works in MRI only because of the way MRI is implemented. The current call's passed block just happens to be available when calling proc, and proc just happens to not care where the block comes from

I was kind-of expecting something like that given how "magic" the whole thing feels. Thanks for telling us that we shouldn't hold our breath, it's appreciated.

So, I'll slowly remove 'proc' from my projects ... It's not as if there was many of them.

@kares kares added this to the Won't Fix milestone Sep 25, 2018
@headius headius closed this as completed Oct 9, 2018
@headius
Copy link
Member

headius commented Oct 9, 2018

@doudou Thanks for understanding! I hope some day CRuby makes it official that these forms are not expected to work, since they're pretty weird anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants