Is there a perl equivalent of .= in ruby/rails?


#1

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.


#2

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.


#3

html << var1.to_s + “this is a test” + whatever.to_s

there is a print method in ruby, p for short


#4

On 3/1/06, cranberry removed_email_address@domain.invalid 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/


#5

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 (removed_email_address@domain.invalid)
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


#6

On Mar 1, 2006, at 1:27 PM, Nithin R. wrote:

On 3/1/06, cranberry removed_email_address@domain.invalid 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. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com


#7

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


#8

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 :slight_smile:

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 (removed_email_address@domain.invalid)
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


#9

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


#10

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

#11

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

#12

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.


#13

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.

  • 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


#14

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?


#15

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 :wink:


#16

Bogdan I. 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 :wink:

LOL, whatever. Grow up.

Joe