Shorter form of concat

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?

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.

Tim H. 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

SpringFlowers AutumnMoon wrote:

Tim H. 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

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}”}

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

On 04.02.2008 10:53, SpringFlowers AutumnMoon wrote:

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

On Feb 4, 2008 7:53 AM, SpringFlowers AutumnMoon
[email protected]
wrote:

puts x.a(y)

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.

On Feb 4, 4:53 am, SpringFlowers AutumnMoon [email protected]
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

On Feb 4, 2008 3:34 PM, Karl von Laudermann [email protected]
wrote:

On Feb 4, 4:53 am, SpringFlowers AutumnMoon [email protected]
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 :slight_smile: I use #map on ranges frequently.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs