Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Relation#select(&:method) does not work because of arity -1 #201

Closed
mikz opened this issue Dec 4, 2012 · 11 comments
Closed

Relation#select(&:method) does not work because of arity -1 #201

mikz opened this issue Dec 4, 2012 · 11 comments

Comments

@mikz
Copy link

mikz commented Dec 4, 2012

Hi,
I've run into issue with https://github.com/ernie/squeel/blob/master/lib/squeel/adapters/active_record/relation_extensions.rb#L170

Because :symbol.to_proc.arity == -1 (at least ruby 1.9.3) it passes relation.select(&:valid?) to DSL.eval and raises Can't create Binding from C level Proc

I believe the check should be extended to check for -1 arity.
Or check http://ruby-doc.org/core-1.9.3/Proc.html#method-i-source_location that it is defined in ruby.

Should I create patch & issue pull request?

@ernie
Copy link
Contributor

ernie commented Dec 4, 2012

I could be misremembering, but I believe this issue was raised once before and I did some checking -- the problem was that arity could == -1 in more situations than this one, and specs broke.

@mikz
Copy link
Author

mikz commented Dec 4, 2012

Sry for editing issue after posting. That might be true. What about checking Proc#source_location ?
It should be nil for non ruby blocks. And Symbol#to_proc is non ruby block.

@the8472
Copy link

the8472 commented Dec 4, 2012

And Symbol#to_proc is non ruby block.

That's implementation-dependent. I think in rubinius it could be a ruby block, considering that a larger fraction of its core methods are written in ruby.
That seems to be too fickle to rely on.

@ernie
Copy link
Contributor

ernie commented Dec 4, 2012

Hm. Might work. Sounds a little weird to me at first glance, though, and potentially temperamental. Since you can do .all.select(&:thing) as an easy workaround, I'd prefer people do that. :/

@mikz
Copy link
Author

mikz commented Dec 4, 2012

Ok, Proc#source_location might fail on other implementations. So I tried arity == -1 and it worked for me.
Don't know how to run specs for all versions, but rspec spec said two failures before my chnages and same after.

mikz@8333095 are my changes

@ernie
Copy link
Contributor

ernie commented Dec 4, 2012

OK, I'll check into it again. Like I said, I was going on memory, so very well could be wrong. :)

@mikz
Copy link
Author

mikz commented Dec 4, 2012

I agree that .all.select(&:odd) looks better. Only reason why I raised this is Rails compatibility.
Btw thanks for awesome gem 👍

@ernie ernie closed this as completed in 7807c3e Dec 4, 2012
ernie added a commit that referenced this issue Dec 5, 2012
@ernie
Copy link
Contributor

ernie commented Dec 5, 2012

So, I now remember the problem. It won't work under 1.8. -1 is returned if you specify a block without params, even if it's not via Symbol#to_proc. Will have to figure out a decent workaround before I can release 1.0.14. :(

@mikz
Copy link
Author

mikz commented Dec 5, 2012

Documentation says it should work (http://www.ruby-doc.org/core-1.8.6/Proc.html#method-i-binding), but indeed
it doesn't. http://bugs.ruby-lang.org/issues/574 changed it to -1 so it is "proper" behaviour.

At least :symbol.to_proc.binding works. But it works also in rubinius (1.9 mode).
In rubinius (Proc.new { }).arity also returns -1 (in 1.8 mode).

I tried several checks (checking if Proc#parameters exists and such), but couldn't get it working.
I believe only way is to check RUBY_VESION :(

On 5. 12. 2012, at 2:54, Ernie Miller notifications@github.com wrote:

So, I now remember the problem. It won't work under 1.8. -1 is returned if you specify a block without params, even if it's not via Symbol#to_proc. Will have to figure out a decent workaround before I can release 1.0.14. :(


Reply to this email directly or view it on GitHub.

@ernie
Copy link
Contributor

ernie commented Dec 5, 2012

Released 1.0.14 last night. ;)

@mikz
Copy link
Author

mikz commented Dec 5, 2012

Nice way! 👍

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

No branches or pull requests

3 participants