[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

shorter form of concat

SpringFlowers AutumnMoon

2/4/2008 1:26:00 AM

the following line will concat "1" all the way to "10"... but is there
a shorter way... like x . y or must it be this long?


p (1..10).inject{|x,y| x.to_s + y.to_s}

also... x.to_s + y
won't cause y to convert to a string?
--
Posted via http://www.ruby-....

9 Answers

Tim Hunter

2/4/2008 1:34:00 AM

0

SpringFlowers AutumnMoon wrote:
> the following line will concat "1" all the way to "10"... but is there
> a shorter way... like x . y or must it be this long?
>
>
> p (1..10).inject{|x,y| x.to_s + y.to_s}

(1..10).to_a.join

>
> also... x.to_s + y
> won't cause y to convert to a string?

No.

--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

SpringFlowers AutumnMoon

2/4/2008 5:23:00 AM

0

Tim Hunter wrote:
> SpringFlowers AutumnMoon wrote:
>> the following line will concat "1" all the way to "10"... but is there
>> a shorter way... like x . y or must it be this long?
>>
>>
>> p (1..10).inject{|x,y| x.to_s + y.to_s}
>
> (1..10).to_a.join


what i mean is, any shorter way to concat two numbers? something
similar to x . y


--
Posted via http://www.ruby-....

Day

2/4/2008 5:35:00 AM

0

This is not pretty, but...

x = 1
y = 10
string = "#{x}#{y}"
puts string => 110

I'm not sure I'd use that in production code, but there it is...


Ben

On Feb 3, 2008 11:23 PM, SpringFlowers AutumnMoon
<summercoolness@gmail.com> wrote:
> Tim Hunter wrote:
> > SpringFlowers AutumnMoon wrote:
> >> the following line will concat "1" all the way to "10"... but is there
> >> a shorter way... like x . y or must it be this long?
> >>
> >>
> >> p (1..10).inject{|x,y| x.to_s + y.to_s}
> >
> > (1..10).to_a.join
>
>
> what i mean is, any shorter way to concat two numbers? something
> similar to x . y
>
>
>
> --
> Posted via http://www.ruby-....
>
>

7stud --

2/4/2008 6:18:00 AM

0

SpringFlowers AutumnMoon wrote:
> Tim Hunter wrote:
>> SpringFlowers AutumnMoon wrote:
>>> the following line will concat "1" all the way to "10"... but is there
>>> a shorter way... like x . y or must it be this long?
>>>
>>>
>>> p (1..10).inject{|x,y| x.to_s + y.to_s}
>>
>> (1..10).to_a.join
>
>
> what i mean is, any shorter way to concat two numbers? something
> similar to x . y

class Fixnum
def a(num)
return sprintf("%s%s", self, num)
end
end

x = 3
y = 4
puts x.a(y)

--output:--
34
--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

2/4/2008 9:54:00 AM

0

7stud -- wrote:

> class Fixnum
> def a(num)
> return sprintf("%s%s", self, num)
> end
> end
>
> x = 3
> y = 4
> puts x.a(y)
>
> --output:--
> 34


thanks. or this one works too:

p (1..10).inject{|x,y| "#{x}#{y}"}
--
Posted via http://www.ruby-....

Moises Trovo

2/4/2008 6:49:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Feb 4, 2008 7:53 AM, SpringFlowers AutumnMoon <summercoolness@gmail.com>
wrote:

> 7stud -- wrote:
>
> > class Fixnum
> > def a(num)
> > return sprintf("%s%s", self, num)
> > end
> > end
> >
> > x = 3
> > y = 4
> > puts x.a(y)
> >
> > --output:--
> > 34
>
>
> thanks. or this one works too:
>
> p (1..10).inject{|x,y| "#{x}#{y}"}
> --
> Posted via http://www.ruby-....
>
>

I think its better with a .to_a.join, depending on what you want it could be
more legible than the .inject one and equally customizable.

Robert Klemme

2/4/2008 7:38:00 PM

0

On 04.02.2008 10:53, SpringFlowers AutumnMoon wrote:
> 7stud -- wrote:
>
>> class Fixnum
>> def a(num)
>> return sprintf("%s%s", self, num)
>> end
>> end
>>
>> x = 3
>> y = 4
>> puts x.a(y)
>>
>> --output:--
>> 34
>
>
> thanks. or this one works too:
>
> p (1..10).inject{|x,y| "#{x}#{y}"}

If you use #inject, then you should rather do

irb(main):003:0> (1..10).inject("") {|s,x| s << x.to_s}
=> "12345678910"

or

irb(main):004:0> require 'stringio'
=> true
irb(main):005:0> (1..10).inject(StringIO.new) {|s,x| s << x}.string
=> "12345678910"


This is - at least in theory - much more efficient than repeated string
interpolation.

Kind regards

robert

Karl von Laudermann

2/4/2008 9:33:00 PM

0

On Feb 4, 4:53 am, SpringFlowers AutumnMoon <summercooln...@gmail.com>
wrote:
> thanks. or this one works too:
>
> p (1..10).inject{|x,y| "#{x}#{y}"}

As does this:

(1..10).map {|n| n.to_s}.join

Todd Benson

2/4/2008 11:03:00 PM

0

On Feb 4, 2008 3:34 PM, Karl von Laudermann <doodpants@mailinator.com> wrote:
> On Feb 4, 4:53 am, SpringFlowers AutumnMoon <summercooln...@gmail.com>
> wrote:
> > thanks. or this one works too:
> >
> > p (1..10).inject{|x,y| "#{x}#{y}"}
>
> As does this:
>
> (1..10).map {|n| n.to_s}.join

Yes :) I use #map on ranges frequently.