Robert Klemme
2/4/2008 7:38:00 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