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.
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.
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 [email protected] 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 D.
Zend Certified Engineer
MySQL Core Certification
http://destiney.com/
Hi –
On Wed, 1 Mar 2006, Nithin R. wrote:
html << var1.to_s + “this is a test” + whatever.to_s
there is a print method in ruby, p for short
p 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 ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)
“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for Rails
On Mar 1, 2006, at 1:27 PM, Nithin R. wrote:
On 3/1/06, cranberry [email protected] 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_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.
html << “#{var1}this is a test#{whatever}”
Even better is to append strings to an Array and join them at the end.
–
Eric H. - [email protected] - 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_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?
Joe
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 ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)
“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for 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
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
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.
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 P. 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.
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
Bogdan I. 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?
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