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.
cranberry (Guest)
on 2006-03-01 23: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.
Noah (Guest)
on 2006-03-01 23: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.
Nithin R. (Guest)
on 2006-03-01 23: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
Greg D. (Guest)
on 2006-03-01 23:30
(Received via mailing list)
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/
unknown (Guest)
on 2006-03-02 00:12
(Received via mailing list)
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
Eric H. (Guest)
on 2006-03-02 02:22
(Received via mailing list)
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
Joe (Guest)
on 2006-03-02 02: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
unknown (Guest)
on 2006-03-02 02: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 (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
Joe (Guest)
on 2006-03-02 03: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
Nithin R. (Guest)
on 2006-03-02 03: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
Matt P. (Guest)
on 2006-03-03 10: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
Bogdan I. (Guest)
on 2006-03-03 10: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.
Carmen -. (Guest)
on 2006-03-03 12:34
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?
Joe (Guest)
on 2006-03-03 13:31
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
Bogdan I. (Guest)
on 2006-03-03 13: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 ;)
Joe (Guest)
on 2006-03-03 21:37
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 ;)

LOL, whatever. Grow up.

Joe
This topic is locked and can not be replied to.