# Named scope lambda string substitution problem

**URL:** <https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804>\
**Category:** Ruby\
**Created:** [November 7, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804 "2008-11-07T15:57:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 7, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/1 "2008-11-07T15:57:27Z")

</div>

Sorry for a cross post with rails, but I think this may be more of a  
ruby question (or rails’s impl?). I have a complex named scope like  
the following.

named\_scope :foo, lambda {|foo, bar|  
foo.each do |d|  
#foo = “this line doesn’t work #{s}”  
baz = "something like this works " + s  
end

```
bar.each do |b|
end

```

{  
:conditions =\> {‘baz’ =\> 1}

}}

It does some pre-processing to calculate the :conditions hash. The  
weird bit is that I get a bizarre syntax error when I try to use  
double quoted strings with substitutions. If I use a syntax such as  
“A”+str+“B” it’s ok, but “A#{str}B” is a syntax error. Any thoughts?

I’ve tried to replicate it in pure ruby lambda functions but wasn’t  
able to, leading me to think it may be related to the named scope.

-Jeff

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 7, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/2 "2008-11-07T19:11:58Z")

</div>

jdwy wrote:

> Sorry for a cross post with rails, but I think this may be more of a  
> ruby question (or rails’s impl?). I have a complex named scope like  
> the following.
> 
> named\_scope :foo, lambda {|foo, bar|  
> foo.each do |d|  
> #foo = “this line doesn’t work #{s}”  
> baz = "something like this works " + s  
> end
> 
> ```
> bar.each do |b|
> end
> 
> ```
> 
> {  
> :conditions =\> {‘baz’ =\> 1}
> 
> }}
> 
> It does some pre-processing to calculate the :conditions hash. The  
> weird bit is that I get a bizarre syntax error when I try to use  
> double quoted strings with substitutions. If I use a syntax such as  
> “A”+str+“B” it’s ok, but “A#{str}B” is a syntax error. Any thoughts?

It’s clearly not a syntax error. Post the _exact_ exception report you  
see, together with the exact code which causes it. If you’re going to  
show just an excerpt, then at least show the line numbers on the excerpt  
so these can be tied up to the exception.

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 7, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/3 "2008-11-07T21:35:44Z")

</div>

Hey Brian,  
This is the syntax error I get. The code I posted is enough to trigger  
it in a simple class. I modified and added line numbers below.

[user@silvia proj-git]$ ruby foo.rb  
foo.rb:8: syntax error, unexpected kDO\_BLOCK, expecting ‘}’  
bar.each do |b|  
^

1 class Foo  
2 named\_scope :foo, lambda {|foo, bar|  
3 foo.each do |d|  
4 foo = “this line doesn’t work #{s}”  
5 baz = "something like this works " + s  
6 end  
7  
8 bar.each do |b|  
9 end  
10 {  
11 :conditions =\> {‘baz’ =\> 1}  
12  
13 }}  
14 end

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 7, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/4 "2008-11-07T22:39:06Z")

</div>

foo :bar {} # doesn’t work.

use doend or parentheses around the arguments.

–  
instance\_variable\_set(%@@%sample@%%@ew@.succ, Class.new(&proc{def  
[self.net](http://self.net);$;,  
$/=‘’,‘/’;%;.fqn-  
cmtkhng;end}));Kernel.send(:“define\_method”,:method\_missing){|  
n,$_|$_\<\<“?kd!jhl”;n=split.map{|q|q.succ}_‘’;puts n.reverse.chomp.tr(_  
%w{" a})}  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 7, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/5 "2008-11-07T22:39:41Z")

</div>

foo :bar {} # doesn’t work.

use doend or parentheses around the arguments.

– instance\_variable\_set(%@@%sample@%%@ew@.succ, Class.new(&proc{def  
[self.net](http://self.net);$;,  
$/=‘’,‘/’;%;.fqn-  
cmtkhng;end}));Kernel.send(:“define\_method”,:method\_missing){|  
n,$_|$_\<\<“?kd!jhl”;n=split.map{|q|q.succ}_‘’;puts n.reverse.chomp.tr(_  
%w{" a})}  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 8, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/6 "2008-11-08T20:28:47Z")

</div>

On Nov 8, 2008, at 20:07, jdwy wrote:

> ah! right you are. that solves the problem. That does seem like a  
> weird ruby oddity. I wonder if there’s a reason for it?  
> Thanks,  
> -Jeff

Sorry about the double post earlier (and lack of special characters),  
Mail.app didn’t want as I.

The reason it doesn’t work is that the parser thinks the {…} means a  
hash instead of a block.

–  
a,b=%Q=Z,O^NPO\r4\_PV\PI\x15^-\x0\v=,email=%%%%c%115%%# Mikael  
Hoilund, CTO  
okay=%#;hmm=(0…a.size).map{|i|((a[i]-email[i]+2)%128).# of [Meta.io](http://Meta.io)  
ApS from  
chr}.join;!email.gsub!‘o’,“%c%c”%[3+?0.\<\<(2),?G.~@];aha=#############  
Denmark  
hmm.scan(/#{‘(.)’\*5}/);!puts(email[1…-12]+aha.shift.zip(\*aha).join)#  
Ruby \<3

---

<div class="post-metadata">

**Author:** ![jdwy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jdwy](https://www.ruby-forum.com/u/jdwy)\
**Post date:** [November 8, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/named-scope-lambda-string-substitution-problem/152804/7 "2008-11-08T20:11:08Z")

</div>

ah! right you are. that solves the problem. That does seem like a  
weird ruby oddity. I wonder if there’s a reason for it?  
Thanks,  
-Jeff
