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

Bug with RubyRegexp when pass it a block. #955

Closed
deivinsontejeda opened this issue Aug 14, 2013 · 4 comments
Closed

Bug with RubyRegexp when pass it a block. #955

deivinsontejeda opened this issue Aug 14, 2013 · 4 comments
Milestone

Comments

@deivinsontejeda
Copy link

@deivinsontejeda deivinsontejeda commented Aug 14, 2013

I was writing a DSL which use Regexp class with MRI (1.9.3 and 2.0.x) works fine but with jruby[1] does not works as expect :(

Leave how I found this issue.

class TestDSL < Regexp
  def initialize(&block)
    instance_eval &block
    super('/some_regex/')
  end
  def some_method(value)
    puts "Output: #{value}"
  end
end

When I running the previous code:
>> TestDSL.new { some_method 'Hi!' }

I get the following output:

ArgumentError: block not supplied
    from org/jruby/RubyBasicObject.java:1709:in `instance_eval'
    from (irb):10:in `initialize'
    from org/jruby/RubyRegexp.java:729:in `new'
    from (irb):17:in `evaluate'
    from org/jruby/RubyKernel.java:1066:in `eval'
    from org/jruby/RubyKernel.java:1409:in `loop'
    from org/jruby/RubyKernel.java:1174:in `catch'
    from org/jruby/RubyKernel.java:1174:in `catch'

With idea to discard some problem in my code I decided test it with a dummy code like follow:

class Foo
  def initialize(&block)
    instance_eval &block
  end
end

When run it:
Foo.new { puts "yay #{self.inspect}" }

I get:

yay #<Foo:0x54bbb2d0>

Looks like works fine.

[1] output of my jruby version jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_51-b11-457-11M4509 [darwin-x86_64]

@ratnikov
Copy link
Contributor

@ratnikov ratnikov commented Aug 14, 2013

Seems like the issue is that https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyRegexp.java#L912 is ignoring the new's block. I'll try to whip out a commit soon enough to fix that.

@headius
Copy link
Member

@headius headius commented Aug 15, 2013

Another place where our allocate+initialize cycle is not being done quite right.

@headius headius closed this in 6729d3b Aug 15, 2013
@headius
Copy link
Member

@headius headius commented Aug 15, 2013

Workaround for you until 1.7.5 is out:

class << Regexp
  def new(*args, &block)
    obj = allocate
    obj.send :initialize, *args, &block
    obj
  end
end

It won't be without some additional overhead, however.

@deivinsontejeda
Copy link
Author

@deivinsontejeda deivinsontejeda commented Aug 15, 2013

@headius thanks for the workaround!

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

Successfully merging a pull request may close this issue.

None yet
3 participants