COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### shorter form of concat

• Follow

```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-forum.com/.

```
 0
Reply Summercoolness (256) 2/4/2008 1:26:25 AM

```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.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

```
 0
Reply TimHunter (698) 2/4/2008 1:34:10 AM

```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-forum.com/.

```
 0
Reply Summercoolness (256) 2/4/2008 5:23:10 AM

```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-forum.com/.
>
>

```
 0
Reply iamday (28) 2/4/2008 5:34:39 AM

```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-forum.com/.

```
 0
Reply bbxx789_05ss (1895) 2/4/2008 6:18:16 AM

```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-forum.com/.

```
 0
Reply Summercoolness (256) 2/4/2008 9:53:50 AM

```[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-forum.com/.
>
>

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.

```
 0
Reply moises.trovo (6) 2/4/2008 6:49:25 PM

```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
```
 0
Reply shortcutter (5766) 2/4/2008 7:38:24 PM

```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
```
 0
Reply doodpants (141) 2/4/2008 9:33:10 PM

```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.

```
 0
Reply caduceass (829) 2/4/2008 11:03:21 PM

9 Replies
46 Views

(page loaded in 0.246 seconds)

5/25/2013 4:56:10 AM

 Reply: