Forum: Ruby on Rails Is there a perl equivalent of .= in ruby/rails??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aa776a6aaefaabca63e989e7042da5e2?d=identicon&s=25 cranberry (Guest)
on 2006-03-01 22:21

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.
8360c3a75305def8f1ac0346f7e3b047?d=identicon&s=25 Noah (Guest)
on 2006-03-01 22:26
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.
720fde074c77f4c9692dd6f05802d581?d=identicon&s=25 Nithin Reddy (jashugan)
on 2006-03-01 22:27
(Received via mailing list)
html << var1.to_s + "this is a test"  + whatever.to_s

there is a print method in ruby, p for short
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2006-03-01 22:30
(Received via mailing list)
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/
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 23:12
(Received via mailing list)
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 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 (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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-03-02 01:22
(Received via mailing list)
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_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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-03-02 01:31
> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-02 01:51
(Received via mailing list)
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
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-03-02 02:04
> 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
720fde074c77f4c9692dd6f05802d581?d=identicon&s=25 Nithin Reddy (jashugan)
on 2006-03-02 02:38
(Received via mailing list)
> >
> > 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
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matt Palmer (Guest)
on 2006-03-03 09:30
(Received via mailing list)
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
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 Bogdan Ionescu (Guest)
on 2006-03-03 09:48
(Received via mailing list)
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.
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-03-03 11:34
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?
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-03-03 12:31
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
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 Bogdan Ionescu (Guest)
on 2006-03-03 12:40
(Received via mailing list)
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 ;)
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-03-03 20:37
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
This topic is locked and can not be replied to.