Kenneth
(Kenneth)
4 February 2008 01:26
1
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/ .
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
Kenneth
(Kenneth)
4 February 2008 05:23
3
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/\ .
Day
(Day)
4 February 2008 05:34
4
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/\ .
7stud
(7stud --)
4 February 2008 06:18
5
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/\ .
Kenneth
(Kenneth)
4 February 2008 09:53
6
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.
···
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/\ .
Robert_K1
(Robert K.)
4 February 2008 19:40
8
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 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}"}
As does this:
(1..10).map {|n| n.to_s}.join
···
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}"}
Yes I use #map on ranges frequently.
···
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