On Wed, Oct 26, 2011 at 4:28 PM, Peter Vandenabeele
<peter / vandenabeele.com> wrote:
> 2011/10/26 Jes=FAs Gabriel y Gal=E1n <jgabrielygalan / gmail.com>
>
>> On Wed, Oct 26, 2011 at 4:04 PM, Marc Heiler <shevegen / linuxmail.org>
>> wrote:
>> > Given is a small .rb file.
>> >
>> > In that .rb file, class Array is modified and a convenience
>> > method is added.
>> >
>> > class Array
>> >
>> > =A0def rand
>> >
>> > I want to limit this modification to only that specific
>> > .rb file though.
>> >
>> > When I use that .rb file in a larger project, I do not want
>> > the modification of class Array to leak out into other .rb
>> > files. Is there a way to limit the scope of the modification?
>> >
>> > Of course I can define a method simply and call that, but:
>> >
>> > =A0array.rand
>> >
>> > Looks better to my eyes than:
>> >
>> > =A0rand(array)
>> >
>> > So I would prefer to be able to do only the array.rand.
>>
>> One solution would be to create a module with the rand method, and
>> extend only the arrays you use in that .rb:
>>
>> # small.rb
>>
>> module Randomizable
>> =A0def rand
>> =A0 =A0 # your implentation
>> =A0end
>> end
>>
>> some_elements =3D [1,2,3,4,5]
>> some_elements.extend(Randomizable)
>> some_elements.rand
>>
>> All other arrays in the system will not be affected.
>>
>> Jesus.
>>
>
>
> I think an equivalent way is to directly open the singleton class of a
> (what extend is doing in your implementation).

Sure, but if you need it in several arrays, the module approach is cleaner.
And I'd say that even for one array is cleaner too :).

Jesus.