Okay, I have a simple question that I haven''t been able to figure out for while. Back in my PHP days I could use print or echo to output data to the browser inside a <? ?> set of tags. In rails I find myself breaking out of ERb then back into it in order to output some value, like so: <% 3.times do |n| %> <%= n %> <% end %> I wish there was a way to do something like: <% 3.times {|n| print n} %> Am I just missing something obvious? Thanks, Carl _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
There''s a concat method. Try <% 3.times{|n| concat n, binding } %> Kent. On 5/27/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Okay, I have a simple question that I haven''t been able to figure out for > while. Back in my PHP days I could use print or echo to output data to the > browser inside a <? ?> set of tags. In rails I find myself breaking out of > ERb then back into it in order to output some value, like so: > > <% 3.times do |n| %> > <%= n %> > <% end %> > > I wish there was a way to do something like: > > <% 3.times {|n| print n} %> > > Am I just missing something obvious? > > Thanks, > Carl > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
So is "binding" the variable I should concat to for every view? On 5/27/05, Kent Sibilev <ksruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There''s a concat method. Try > <% 3.times{|n| concat n, binding } %> > > Kent. > > On 5/27/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Okay, I have a simple question that I haven''t been able to figure out > for > > while. Back in my PHP days I could use print or echo to output data to > the > > browser inside a <? ?> set of tags. In rails I find myself breaking out > of > > ERb then back into it in order to output some value, like so: > > > > <% 3.times do |n| %> > > <%= n %> > > <% end %> > > > > I wish there was a way to do something like: > > > > <% 3.times {|n| print n} %>_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The +binding+ variable is a variable that exists in all scopes that can be passed around as a parameter if necessary. It basically says "These are the variables that the scope had access to when I was passed as a parameter". In that way, the variable "n" in your example can be accessed by code that normally has no access to it. Although I don''t understand the internals of ERb, it appears that ERb can''t just "know" what the context (ie: scope) of your variables are while parsing the code, so it relies on <%= or else concat(your_var, binding) to tell it how to access the value of the variable to add to the generated output stream. Clear as mud? Duane Johnson (canadaduane) On May 27, 2005, at 9:49 PM, Carl Youngblood wrote:> So is "binding" the variable I should concat to for every view? > > On 5/27/05, Kent Sibilev <ksruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote: > There''s a concat method. Try > <% 3.times{|n| concat n, binding } %> > > Kent. > > On 5/27/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Okay, I have a simple question that I haven''t been able to figure > out for > > while. Back in my PHP days I could use print or echo to output > data to the > > browser inside a <? ?> set of tags. In rails I find myself > breaking out of > > ERb then back into it in order to output some value, like so: > > > > <% 3.times do |n| %> > > <%= n %> > > <% end %> > > > > I wish there was a way to do something like: > > > > <% 3.times {|n| print n} %> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the explanation. I like to try and understand what is actually happening in the code instead of just accepting that it works on faith, so to speak :) Carl On 5/28/05, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The +binding+ variable is a variable that exists in all scopes that can be > passed around as a parameter if necessary. It basically says "These are the > variables that the scope had access to when I was passed as a parameter". In > that way, the variable "n" in your example can be accessed by code that > normally has no access to it. > Although I don''t understand the internals of ERb, it appears that ERb > can''t just "know" what the context (ie: scope) of your variables are while > parsing the code, so it relies on <%= or else concat(your_var, binding) to > tell it how to access the value of the variable to add to the generated > output stream. > > Clear as mud? >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails