Max Williams wrote:
> Here's a little ruby exercise - i'm a bit brain damaged today and can't
> think of a nice way to do this.
> 
> I want a method where you pass a name as a string and it returns 'copy
> of x' or 'copy 2 of x' eg
> 
> new_name("My Lesson")
>   => "Copy of My Lesson"
> 
> new_name("Copy of My Lesson")
>   => "Copy 2 of My Lesson"
> 
> new_name("Copy 10 of My Lesson")
>  => "Copy 11 of My Lesson"
> 
> I can only come up with horrible looking solutions but i feel there's a
> nice way to do it.  Anyone?
> 
> cheers
> max

Use a closure to keep track of how many copies you've made:

def copy(string)
   x = 0
   p = lambda do
     x += 1
     if x == 1
       "Copy of #{string}"
     else
       "Copy #{x} of #{string}"
     end
   end
   p
end

c = copy("My Lesson")

puts c.call
puts c.call
puts c.call


-- 
RMagick: http://rmagick.rubyforge.org/