On 17 Aug 2010, at 15:36, Brian Candler wrote:

> Iain Barnett wrote:
>> So why did it produce two different outputs even though my patch didn't 
>> add a rand
> 
> I've no idea, because you haven't shown the source of your 
> "local_methods" method,

class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end


> nor the exact sequence of operations to get to 
> that point.
> 

The exact sequence is below, it's what I posted earlier.

> If you were in a single irb session, and did load 'extensions/Array.rb' 
> after changing Array.rb, then any methods added to Array by the old 
> version will still be present in Array, even if the new Array.rb doesn't 
> define them.

I quit the irb session, I quit the terminal, I made sure there was no ruby process running on the system.

> 
>> and why doesn't my patch even seem to work anymore?
> 
> "Doesn't work" is not a helpful description of an error.

There is no error, I already gave a full description and everything that came back except for local_methods.

> 
> Post your current code (preferably boiled down to the smallest runnable 
> example which replicates it). Show exactly what you type, exactly what ou see - results and/or error message - and what you were expecting to 
> see.

Verbatim (again):

$ irb
> r = [1..99, 100..199]
=> [1..99, 100..199]
> xs = r.local_methods 
=> [:, :, :, :-, :, :=>, :[], :[], :all?, :any?, :assoc, :at, :clear, :collect, :collect!, :combination, :compact, :compact!, :concat, :count, :cycle, :delete, :delete_at, :delete_if, :detect, :drop, :drop_while, :each, :each_cons, :each_index, :each_slice, :each_with_index, :each_with_object, :empty?, :entries, :fetch, :fill, :find, :find_all, :find_index, :first, :flatten, :flatten!, :fold, :grep, :group_by, :include?, :index, :inject, :insert, :join, :last, :length, :map, :map!, :max, :max_by, :member?, :min, :min_by, :minmax, :minmax_by, :none?, :one?, :pack, :partition, :permutation, :pop, :product, :push, :rassoc, :reduce, :reject, :reject!, :replace, :reverse, :reverse!, :reverse_each, :rindex, :sample, :select, :shift, :shuffle, :shuffle!, :size, :slice, :slice!, :sort, :sort!, :sort_by, :take, :take_while, :to_a, :to_ary, :transpose, :uniq, :uniq!, :unshift, :values_at, :zip, :]
> load 'extensions/Array.rb'
=> true
> r.local_methods - xs
=> [:rand]

This is extensions/Array.rb

class Array

 def index_in_range( n )
   self.each_with_index do |x, i|
     return i if x === n
   end
   return nil
 end


end

I'm not expecting to see rand in the list of public methods brought back by r.local_methods. I've even since moved the extensions/Array.rb to a new location.



Regards,
Iain