[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

named scope lambda string substitution problem

jdwy

11/7/2008 2:54:00 PM

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
6 Answers

Brian Candler

11/7/2008 6:10:00 PM

0

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.
--
Posted via http://www.ruby-....

jdwy

11/7/2008 8:33:00 PM

0

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


On Nov 7, 1:09 pm, Brian Candler <b.cand...@pobox.com> wrote:
> 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.
> --
> Posted viahttp://www.ruby-....

Mikael Høilund

11/7/2008 9:34:00 PM

0

foo :bar {} # doesn't work.

use doend or parentheses around the arguments.


--
instance_variable_set(%@\@%sample@%%@ew@.succ, Class.new(&proc{def
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})}
me@example.net


Mikael Høilund

11/7/2008 9:34:00 PM

0

foo :bar {} # doesn't work.

use doend or parentheses around the arguments.


-- instance_variable_set(%@\@%sample@%%@ew@.succ, Class.new(&proc{def
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})}
me@example.net


jdwy

11/8/2008 7:09:00 PM

0

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

On Nov 7, 4:34 pm, Mikael Høilund <mik...@hoilund.org> wrote:
> foo :bar {} # doesn't work.
>
> use doend or parentheses around the arguments.
>
> -- instance_variable_set(%@\@%sample@%%@ew@.succ, Class.new(&proc{def  
> 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})}
> m...@example.net

Mikael Høilund

11/8/2008 7:26:00 PM

0

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), =20=

Mail.app didn't want as I.

The reason it doesn't work is that the parser thinks the {=85} means a =20=

hash instead of a block.

--=20
a,b=3D%Q=3DZ,O^NPO\r4_PV\\PI\x15^-\x0\v=3D,email=3D%\%%%c\%115%%# Mikael =
=20
Hoilund, CTO
okay=3D%#;hmm=3D(0...a.size).map{|i|((a[i]-email[i]+2)%128).# of Meta.io =
=20
ApS from
chr}.join;!email.gsub!'o',"%c%c"%[3+?0.<<(2),?G.~@];aha=3D############# =20=

Denmark
hmm.scan(/#{'(.)'*5}/);!puts(email[1..-12]+aha.shift.zip(*aha).join)# =20=

Ruby <3