I am trying to understand how to work with a single quote within an eval such as: assigned_string = '''' name = "Addy''s" eval("assigned_string=''#{name}''") SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end assigned_string=''Addy''s'' ^ Ok, this error makes sense, but when I try to escape the single quote I also get an error: eval("assigned_string=''#{name.gsub(/''/,"\''")}''") SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end assigned_string=''Addy''s'' However if I want to just remove the single quote I am fine and get no error: eval("assigned_string=''#{name.gsub(/''/,"")}''") Is there a different way to do such an eval? In actuality what I am doing with this phrase is to assign hash params to the attributes of a class: eval("background_process_status.#{key.to_s}=''#{value}''") -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Apr 9, 2011 at 10:37 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> I am trying to understand how to work with a single quote within an eval > such as: > > assigned_string = '''' > name = "Addy''s" > eval("assigned_string=''#{name}''") > > SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end > assigned_string=''Addy''s'' > ^ > Ok, this error makes sense, but when I try to escape the single quote I > also get an error: > > eval("assigned_string=''#{name.gsub(/''/,"\''")}''") > SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end > assigned_string=''Addy''s'' > > However if I want to just remove the single quote I am fine and get no > error: > eval("assigned_string=''#{name.gsub(/''/,"")}''") > > > Is there a different way to do such an eval? In actuality what I am doing > with this phrase is to assign hash params to the attributes of a class: > > eval("background_process_status.#{key.to_s}=''#{value}''") > >Does it need to use eval? If not, you can just: background_process.send("#{key.to_s}=", value)> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
try this eval("assigned_string=#{name}"),am not sure may be it ''l work -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 9, 11:37 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> Is there a different way to do such an eval? In actuality what I am doing > with this phrase is to assign hash params to the attributes of a class: > > eval("background_process_status.#{key.to_s}=''#{value}''")I''d *highly* recommend that you not do this. Using send is not only more efficient, it''s far safer - for instance, what happens if somebody sends your code a value like ''; `rm -rf *`;'' This will be syntactically valid, and will make quite a mess... --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Apr 10, 2011 at 9:51 AM, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Apr 9, 11:37 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > Is there a different way to do such an eval? In actuality what I am doing > > with this phrase is to assign hash params to the attributes of a class: > > > > eval("background_process_status.#{key.to_s}=''#{value}''") > > I''d *highly* recommend that you not do this. Using send is not only > more efficient, it''s far safer - for instance, what happens if > somebody sends your code a value like > > ''; `rm -rf *`;'' > > This will be syntactically valid, and will make quite a mess... >Thanks all... yeah, I will use send, I forgot about that and you are right, it gets yucky very fast when things like single quotes and who knows what else get added. Also, on this answer: eval("assigned_string=#{name}") , no it does not work, this is what I had originally but if you output the internal string it looks like "assigned_string=david" which of course errors out unless what is in ''name'' is a non string type.> > --Matt Jones > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 10, 4:38 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> On Sun, Apr 10, 2011 at 9:51 AM, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Apr 9, 11:37 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > Is there a different way to do such an eval? In actuality what I am doing > > > with this phrase is to assign hash params to the attributes of a class: > > > > eval("background_process_status.#{key.to_s}=''#{value}''") > > > I''d *highly* recommend that you not do this. Using send is not only > > more efficient, it''s far safer - for instance, what happens if > > somebody sends your code a value like > > > ''; `rm -rf *`;'' > > > This will be syntactically valid, and will make quite a mess... > > Thanks all... yeah, I will use send, I forgot about that and you are right, > it gets yucky very fast when things like single quotes and who knows what > else get added.For what it''s worth I suspect that the problem was that you needed to escape the \ in your substitution ( ie "\\''") Fred> > Also, on this answer: eval("assigned_string=#{name}") , no it does not work, > this is what I had originally but if you output the internal string it looks > like "assigned_string=david" which of course errors out unless what is in > ''name'' is a non string type. > > > > > > > --Matt Jones > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Apr 10, 2011 at 10:43 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Apr 10, 4:38 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > On Sun, Apr 10, 2011 at 9:51 AM, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Apr 9, 11:37 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > Is there a different way to do such an eval? In actuality what I am > doing > > > > with this phrase is to assign hash params to the attributes of a > class: > > > > > > eval("background_process_status.#{key.to_s}=''#{value}''") > > > > > I''d *highly* recommend that you not do this. Using send is not only > > > more efficient, it''s far safer - for instance, what happens if > > > somebody sends your code a value like > > > > > ''; `rm -rf *`;'' > > > > > This will be syntactically valid, and will make quite a mess... > > > > Thanks all... yeah, I will use send, I forgot about that and you are > right, > > it gets yucky very fast when things like single quotes and who knows what > > else get added. > > For what it''s worth I suspect that the problem was that you needed to > escape the \ in your substitution ( ie "\\''") >I just tried this and thought for sure you were right. But no! So let me step back, really I fail to understand why if I have a param[key] value of "Addy''s" that Ruby can not handle it on its own, and much less escaped by ("\''" or "\\''"): account.name = "Addy''s" BackgroundProcessStatus.update(:status => "Processing Account name: #{ account.name}") class BackgroundProcessStatus < ActiveRecord::Base def self.update(params) background_process_status = BackgroundProcessStatus.first || BackgroundProcessStatus.new params.keys.each do |key| background_process_status.send("#{key.to_s}=", params[key]) end end end The delayed_job output is: SyntaxError: (eval):2: syntax error, unexpected tIDENTIFIER, expecting $end ...Processing Account name: Addy''s Harbor Dodge'' ^ - 0 failed attempts I dont know if this has to do with it running as a job but if so in the dark as to why that would be as this seems to be pretty basic Ruby.> > Fred > > > > Also, on this answer: eval("assigned_string=#{name}") , no it does not > work, > > this is what I had originally but if you output the internal string it > looks > > like "assigned_string=david" which of course errors out unless what is in > > ''name'' is a non string type. > > > > > > > > > > > > > --Matt Jones > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Apr 10, 2011 at 11:06 AM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> > > On Sun, Apr 10, 2011 at 10:43 AM, Frederick Cheung < > frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> >> On Apr 10, 4:38 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: >> > On Sun, Apr 10, 2011 at 9:51 AM, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > >> > > On Apr 9, 11:37 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: >> > > > Is there a different way to do such an eval? In actuality what I am >> doing >> > > > with this phrase is to assign hash params to the attributes of a >> class: >> > >> > > > eval("background_process_status.#{key.to_s}=''#{value}''") >> > >> > > I''d *highly* recommend that you not do this. Using send is not only >> > > more efficient, it''s far safer - for instance, what happens if >> > > somebody sends your code a value like >> > >> > > ''; `rm -rf *`;'' >> > >> > > This will be syntactically valid, and will make quite a mess... >> > >> > Thanks all... yeah, I will use send, I forgot about that and you are >> right, >> > it gets yucky very fast when things like single quotes and who knows >> what >> > else get added. >> >> For what it''s worth I suspect that the problem was that you needed to >> escape the \ in your substitution ( ie "\\''") >> > > I just tried this and thought for sure you were right. But no! So let me > step back, really I fail to understand why if I have a param[key] value of > "Addy''s" that Ruby can not handle it on its own, and much less escaped by > ("\''" or "\\''"): >I found the problem --- seems that the delayed_job process must be restarted if code is updated. I had updated the code but the old code was seemingly being executed.> > account.name = "Addy''s" > BackgroundProcessStatus.update(:status => "Processing Account name: #{ > account.name}") > > class BackgroundProcessStatus < ActiveRecord::Base > def self.update(params) > background_process_status = BackgroundProcessStatus.first || > BackgroundProcessStatus.new > params.keys.each do |key| > background_process_status.send("#{key.to_s}=", params[key]) > end > end > end > > > The delayed_job output is: > SyntaxError: (eval):2: syntax error, unexpected tIDENTIFIER, expecting > $end > ...Processing Account name: Addy''s Harbor Dodge'' > ^ - 0 failed attempts > > I dont know if this has to do with it running as a job but if so in the > dark as to why that would be as this seems to be pretty basic Ruby. > > > >> >> Fred >> > >> > Also, on this answer: eval("assigned_string=#{name}") , no it does not >> work, >> > this is what I had originally but if you output the internal string it >> looks >> > like "assigned_string=david" which of course errors out unless what is >> in >> > ''name'' is a non string type. >> > >> > >> > >> > >> > >> > > --Matt Jones >> > >> > > -- >> > > You received this message because you are subscribed to the Google >> Groups >> > > "Ruby on Rails: Talk" group. >> > > To post to this group, send email to >> rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > > To unsubscribe from this group, send email to >> > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > > For more options, visit this group at >> > >http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi David, On 10 Apr 2011, at 04:37, David Kahn wrote:> Is there a different way to do such an eval? In actuality what I am doing with this phrase is to assign hash params to the attributes of a class: > > eval("background_process_status.#{key.to_s}=''#{value}''")Assuming you have attribute writers defined for your attributes: background_process_status.send("#{key}=", value) would be better, I think. Regards, Tony. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.