I want to make a for loop that concats strings onto a variable: html .= var1 + "this is a test" + whatever What is the syntax that does this in ruby/rails? Also, is there a ''print'' method in ruby? Because when I try to use render_text it will only let me use this once. -- Posted via http://www.ruby-forum.com/.
cranberry wrote:> > > I want to make a for loop that concats strings onto a variable: > > html .= var1 + "this is a test" + whatever > > What is the syntax that does this in ruby/rails? > > Also, is there a ''print'' method in ruby? Because when I try to use > render_text it will only let me use this once.html += var1 + "this is a test" + whatever note of course that if var1 and whatever are not strings, you''ll need to call to_s on them to convert them to strings. -- Posted via http://www.ruby-forum.com/.
Nithin Reddy
2006-Mar-01 21:27 UTC
[Rails] Is there a perl equivalent of .= in ruby/rails??
html << var1.to_s + "this is a test" + whatever.to_s there is a print method in ruby, p for short On 3/1/06, cranberry <miriamraphael@yahoo.com> wrote:> > > I want to make a for loop that concats strings onto a variable: > > html .= var1 + "this is a test" + whatever > > What is the syntax that does this in ruby/rails? > > Also, is there a ''print'' method in ruby? Because when I try to use > render_text it will only let me use this once. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Greg Donald
2006-Mar-01 21:29 UTC
[Rails] Is there a perl equivalent of .= in ruby/rails??
On 3/1/06, cranberry <miriamraphael@yahoo.com> wrote:> I want to make a for loop that concats strings onto a variable: > > html .= var1 + "this is a test" + whatever > > What is the syntax that does this in ruby/rails?<< or +> Also, is there a ''print'' method in ruby? Because when I try to use > render_text it will only let me use this once.puts, logger.info, logger.warn, STDERR, STDOUT -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/
dblack@wobblini.net
2006-Mar-01 22:12 UTC
[Rails] Is there a perl equivalent of .= in ruby/rails??
Hi -- On Wed, 1 Mar 2006, Nithin Reddy wrote:> html << var1.to_s + "this is a test" + whatever.to_s > > there is a print method in ruby, p for shortp and print aren''t the same as each other. obj.p is equivalent to: puts obj.inspect, so you get the inspect-style string. print gives you the object''s to_s representation of itself. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
On Mar 1, 2006, at 1:27 PM, Nithin Reddy wrote:> On 3/1/06, cranberry <miriamraphael@yahoo.com> wrote: > >> I want to make a for loop that concats strings onto a variable: >> >> html .= var1 + "this is a test" + whatever >> >> What is the syntax that does this in ruby/rails? > > html << var1.to_s + "this is a test" + whatever.to_sYou generally want to avoid String#+ in Ruby because you create extra copies that must be garbage collected. Instead use interpolation or String#<< all the way through. html << "#{var1}this is a test#{whatever}" Even better is to append strings to an Array and join them at the end. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com
> html << var1.to_s + "this is a test" + whatever.to_sYeck, I hate having to do that. Doesn''t Ruby/Rails have a concat operator that doesn''t require to_s casts to avoid errors? Joe -- Posted via http://www.ruby-forum.com/.
dblack@wobblini.net
2006-Mar-02 00:51 UTC
[Rails] Re: Is there a perl equivalent of .= in ruby/rails??
Hi -- On Thu, 2 Mar 2006, Joe wrote:> >> html << var1.to_s + "this is a test" + whatever.to_s > > Yeck, I hate having to do that. Doesn''t Ruby/Rails have a concat > operator that doesn''t require to_s casts to avoid errors?If you define a to_str method, that method will be called in situations where a string is called for: class C def to_str "a C object" end end puts "I am " << C.new # I am a C object It''s generally easier to use to_s :-) Another thing you can do is interpolation: html << "#{var1}this is a test#{whatever}" which will give you the to_s representation of var1 and whatever. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
Joe
2006-Mar-02 01:04 UTC
[Rails] Re: Re: Is there a perl equivalent of .= in ruby/rails??
> Another thing you can do is interpolation: > > html << "#{var1}this is a test#{whatever}" > > which will give you the to_s representation of var1 and whatever.That''s what I currently do, which leads to another pet peeve of mine ;). "$var" would be nice. Joe -- Posted via http://www.ruby-forum.com/.
Nithin Reddy
2006-Mar-02 01:37 UTC
[Rails] Is there a perl equivalent of .= in ruby/rails??
> > > > html << var1.to_s + "this is a test" + whatever.to_s > > You generally want to avoid String#+ in Ruby because you create extra > copies that must be garbage collected. Instead use interpolation or > String#<< all the way through. >Extra copies of what? How is doing this any better (about not creating extra copies)? html << var << "this is a test" << whatever TIA - N
Matt Palmer
2006-Mar-03 08:29 UTC
[Rails] Re: Re: Is there a perl equivalent of .= in ruby/rails??
On Thu, Mar 02, 2006 at 02:04:17AM +0100, Joe wrote:> > > Another thing you can do is interpolation: > > > > html << "#{var1}this is a test#{whatever}" > > > > which will give you the to_s representation of var1 and whatever. > > That''s what I currently do, which leads to another pet peeve of mine ;). > "$var" would be nice.If you want to write PHP or Perl, you''re free to do so. - Matt
Bogdan Ionescu
2006-Mar-03 08:46 UTC
[Rails] Re: Re: Is there a perl equivalent of .= in ruby/rails??
Maybe Matz could completely change ruby, to act like perl. If the guy wants $var and hates to_s, Ruby has got to change. Bogdan If you want to write PHP or Perl, you''re free to do so.> > - Matt > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060303/df446b30/attachment-0001.html
carmen
2006-Mar-03 10:34 UTC
[Rails] Re: Re: Re: Is there a perl equivalent of .= in ruby/rails??
Bogdan Ionescu wrote:> Maybe Matz could completely change ruby, to act like perl. If the guy > wants > $var and hates to_s, Ruby has got to change.or maybe make your php_print method via some .gsub(/\$([a-zA-z_]+)/,"\#{\1}") tricks? -- Posted via http://www.ruby-forum.com/.
Joe
2006-Mar-03 11:31 UTC
[Rails] Re: Re: Is there a perl equivalent of .= in ruby/rails??
Matt Palmer wrote:> On Thu, Mar 02, 2006 at 02:04:17AM +0100, Joe wrote: >> >> > Another thing you can do is interpolation: >> > >> > html << "#{var1}this is a test#{whatever}" >> > >> > which will give you the to_s representation of var1 and whatever. >> >> That''s what I currently do, which leads to another pet peeve of mine ;). >> "$var" would be nice. > > If you want to write PHP or Perl, you''re free to do so. > > - Matt> Maybe Matz could completely change ruby, to act like perl. If the guywants $var and hates to_s, Ruby has got to change. Um, yeah, mmkay. You dudes like typing #{var}... And BTW, haven''t you seen all those Perl-like vars in Ruby? And Matz is changing Ruby - go see his "Ruby Sucks" slides. Joe -- Posted via http://www.ruby-forum.com/.
Bogdan Ionescu
2006-Mar-03 11:37 UTC
[Rails] Re: Re: Is there a perl equivalent of .= in ruby/rails??
Yeah, I like beer, girls and typing #{var} (the order is random) I humbly recognize the superiority of $var over #{var}. You''re so much cooler than I am ;) On 3/3/06, Joe <joe@yahoo.com> wrote:> > Matt Palmer wrote: > > On Thu, Mar 02, 2006 at 02:04:17AM +0100, Joe wrote: > >> > >> > Another thing you can do is interpolation: > >> > > >> > html << "#{var1}this is a test#{whatever}" > >> > > >> > which will give you the to_s representation of var1 and whatever. > >> > >> That''s what I currently do, which leads to another pet peeve of mine > ;). > >> "$var" would be nice. > > > > If you want to write PHP or Perl, you''re free to do so. > > > > - Matt > > > Maybe Matz could completely change ruby, to act like perl. If the guy > wants $var and hates to_s, Ruby has got to change. > > Um, yeah, mmkay. You dudes like typing #{var}... > > And BTW, haven''t you seen all those Perl-like vars in Ruby? And Matz is > changing Ruby - go see his "Ruby Sucks" slides. > > Joe > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060303/9d0578f9/attachment.html
Joe
2006-Mar-03 19:37 UTC
[Rails] Re: Re: Re: Is there a perl equivalent of .= in ruby/rails??
Bogdan Ionescu wrote:> Yeah, I like beer, girls and typing #{var} (the order is random) > I humbly recognize the superiority of $var over #{var}. > You''re so much cooler than I am ;)LOL, whatever. Grow up. Joe -- Posted via http://www.ruby-forum.com/.