# How can I write this method in one line?

**URL:** <https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106>\
**Category:** Ruby\
**Created:** [February 14, 2008, 11:09am UTC](https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106 "2008-02-14T11:09:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 14, 2008, 11:09am UTC](https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106/1 "2008-02-14T11:09:06Z")

</div>

def gen\_indices\_string num  
string = “”  
num.times{|i| string\<\<(i+1).to\_s}  
return string  
end

I have a feeling that stuff can be written in a single line using some  
of the ruby API. Can anyone help?

thanks

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 14, 2008, 11:26am UTC](https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106/2 "2008-02-14T11:26:26Z")

</div>

Jigar G. wrote:

> def gen\_indices\_string num  
> string = “”  
> num.times{|i| string\<\<(i+1).to\_s}  
> return string  
> end
> 
> I have a feeling that stuff can be written in a single line using some  
> of the ruby API. Can anyone help?

(1…num).to\_a.join  
or  
(1…num).inject("") {|str,i| str \<\< i.to\_s}

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 14, 2008, 11:36am UTC](https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106/3 "2008-02-14T11:36:05Z")

</div>

Sebastian H. wrote:

> Jigar G. wrote:
> 
> > def gen\_indices\_string num  
> > string = “”  
> > num.times{|i| string\<\<(i+1).to\_s}  
> > return string  
> > end
> > 
> > I have a feeling that stuff can be written in a single line using some  
> > of the ruby API. Can anyone help?
> 
> (1…num).to\_a.join  
> or  
> (1…num).inject("") {|str,i| str \<\< i.to\_s}

thanks, I knew there was a better way 🙂

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 14, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/how-can-i-write-this-method-in-one-line/129106/4 "2008-02-14T13:02:46Z")

</div>

irb(main):033:0\> num = 10  
=\> 10

irb(main):037:0\> “%s”\*num % (1…num).to\_a  
=\> “12345678910”

irb(main):036:0\> “%4s”\*num % (1…num).to\_a  
=\> " 1 2 3 4 5 6 7 8 9 10"

irb(main):035:0\> “%-4s”\*num % (1…num).to\_a  
=\> "1 2 3 4 5 6 7 8 9 10 "

irb(main):034:0\> ([“%4s”]\*num).join(“|”) % (1…num).to\_a  
=\> " 1| 2| 3| 4| 5| 6| 7| 8| 9| 10"

2008/2/14, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
