[#370825] Syntax error — Anthony Ob <vidgametester@...>

ERROR

17 messages 2010/10/01
[#370828] Re: Syntax error — Alex Stahl <astahl@...5.com> 2010/10/01

What are you expecting the "x:y" statement to do? I ask because I'm not

[#370844] how can we make a ruby compiler — Robin <r@...1.net>

how can we make a thing that compiles ruby into c++ source code?

50 messages 2010/10/01
[#370896] Re: how can we make a ruby compiler — Ryan Davis <ryand-ruby@...> 2010/10/02

[#371096] Re: how can we make a ruby compiler — Tony Arcieri <tony.arcieri@...> 2010/10/05

On Fri, Oct 1, 2010 at 7:52 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

[#371120] Re: how can we make a ruby compiler — Clifford Heath <no@...> 2010/10/05

Tony Arcieri wrote:

[#371127] Re: how can we make a ruby compiler — Michal Suchanek <hramrach@...> 2010/10/05

On 5 October 2010 07:10, Clifford Heath <no@spam.please.net> wrote:

[#371129] Re: how can we make a ruby compiler — Samuel Williams <space.ship.traveller@...> 2010/10/05

[#371130] Re: how can we make a ruby compiler — Ryan Davis <ryand-ruby@...> 2010/10/05

[#370878] New to Ruby, Looking for Help With Basic Program — Mica Koizumi <monkeymica@...>

I am teaching myself Ruby and am trying to figure out why this program

12 messages 2010/10/01

[#370897] Ruby String: How do I strip anything between two parenthesis — Frank Guerino <frank.guerino@...>

Hi,

9 messages 2010/10/02

[#370912] The Third Ruby - Ever Comes Out at Night? — Mike Stephens <rubfor@...>

Often you see that Ruby can be object-oriented, functional or

19 messages 2010/10/02
[#370915] Re: The Third Ruby - Ever Comes Out at Night? — elise huard <huard.elise@...> 2010/10/02

I guess you could work only with modules and class methods, and avoid

[#370916] Re: The Third Ruby - Ever Comes Out at Night? — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2010/10/02

On Sat, Oct 2, 2010 at 11:27 AM, elise huard <huard.elise@gmail.com> wrote:

[#370918] Re: The Third Ruby - Ever Comes Out at Night? — elise huard <huard.elise@...> 2010/10/02

2010/10/2 Jes俍 Gabriel y Gal疣 <jgabrielygalan@gmail.com>:

[#370919] Re: The Third Ruby - Ever Comes Out at Night? — Mike Stephens <rubfor@...> 2010/10/02

[#370952] Pass by reference and copy on write — Ralph Shnelvar <ralphs@...32.com>

I see

24 messages 2010/10/02
[#370955] Re: Pass by reference and copy on write — Josh Cheek <josh.cheek@...> 2010/10/03

On Sat, Oct 2, 2010 at 6:41 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

[#370958] Re: Pass by reference and copy on write — Caleb Clausen <vikkous@...> 2010/10/03

On 10/2/10, Josh Cheek <josh.cheek@gmail.com> wrote:

[#370964] ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...>

I'm trying to figure out what's so cool about Ruby. I need to create a

49 messages 2010/10/03
[#370982] Re: ODBC app in Ruby - I don't believe it. — Luis Lavena <luislavena@...> 2010/10/03

On Oct 3, 4:26m, Ed Reed <joebanana...@gmail.com> wrote:

[#370996] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/03

WOW! Thanks for all the responses and please accept my apologies for

[#371079] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/04

Okay I've decided to start from scratch,... again. It's the start of a

[#371082] Re: ODBC app in Ruby - I don't believe it. — Jeremy Bopp <jeremy@...> 2010/10/04

On 10/4/2010 1:30 PM, Ed Reed wrote:

[#371087] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/04

My mistake on the gem commands. I did use the correct ones with dbd

[#371102] Re: ODBC app in Ruby - I don't believe it. — Jeremy Bopp <jeremy@...> 2010/10/05

On 10/04/2010 04:29 PM, Ed Reed wrote:

[#371195] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/05

The history.txt file for the mysql gem says

[#371209] Re: ODBC app in Ruby - I don't believe it. — Dave Howell <groups.2009a@...> 2010/10/06

[#371275] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/06

Thanks for the extensive reply Dave. I certainly appreciate it.

[#371330] Re: ODBC app in Ruby - I don't believe it. — Brian Candler <b.candler@...> 2010/10/07

Ed Reed wrote:

[#371455] Re: ODBC app in Ruby - I don't believe it. — Ed Reed <joebananas10@...> 2010/10/08

Brian Candler wrote:

[#371413] Re: ODBC app in Ruby - I don't believe it. — David Masover <ninja@...> 2010/10/08

On Wednesday, October 06, 2010 02:40:38 am Dave Howell wrote:

[#371690] Re: ODBC app in Ruby - I don't believe it. — Dave Howell <groups.2009a@...> 2010/10/13

[#371765] Re: ODBC app in Ruby - I don't believe it. — David Masover <ninja@...> 2010/10/13

On Tuesday, October 12, 2010 08:51:21 pm Dave Howell wrote:

[#370991] install ruby on the mac — Basi Lambanog <basi.lambanog.tuba@...>

hello,

14 messages 2010/10/03

[#371020] save only first line from string? — Terry Michaels <spare@...>

Hi. What's the most simple and elegant way to remove all the contents of

21 messages 2010/10/04

[#371023] How to suppress display of specific code in irb?? — Don Norcott <dnorcott@...>

I am very new to ruby (but a retired experienced C programmer) and am

14 messages 2010/10/04

[#371049] how do i delete files in particular directoryin ruby ??? — Amit Tomar <amittomer25@...>

Hii all,

23 messages 2010/10/04
[#371052] Re: how do i delete files in particular directoryin ruby ??? — Stefano Crocco <stefano.crocco@...> 2010/10/04

On Monday 04 October 2010, Amit Tomar wrote:

[#371069] Re: how do i delete files in particular directoryin ruby ??? — Robert Klemme <shortcutter@...> 2010/10/04

On Mon, Oct 4, 2010 at 2:27 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:

[#371181] How can I count number of elements in an HTML page — Paul <tester.paul@...>

Hi there, I'm using net/http to retrieve some html pages and now I

11 messages 2010/10/05

[#371221] setting local variables in a binding — Martin DeMello <martindemello@...>

Why does this not work?

15 messages 2010/10/06

[#371226] XML-RPC WEBrick problem (error during method invocation) — Nikita Kuznetsov <moog_master@...>

I have a university assignement, and i am stuck. I am supposed to create

10 messages 2010/10/06

[#371239] "map" a deeply nested structure: Object#deep_map — Guido De Rosa <guidoderosa@...>

Hi,

11 messages 2010/10/06
[#371241] Re: "map" a deeply nested structure: Object#deep_map — Robert Klemme <shortcutter@...> 2010/10/06

On Wed, Oct 6, 2010 at 4:43 PM, Guido De Rosa <guidoderosa@gmail.com> wrote:

[#371250] A Real World example for Ruby to "compiled" version discussion — Philip Rhoades <phil@...>

People,

10 messages 2010/10/06

[#371286] Why does Module#include exclude the module's metaclass? — John Mair <jrmair@...>

When classes are inherited in Ruby the singleton classes are also

11 messages 2010/10/06

[#371533] Why does a lot of code not include parenthesis? — egervari <ken.egervari@...>

I just started playing around with ruby and rails, and one thing I've

32 messages 2010/10/11
[#371534] Re: Why does a lot of code not include parenthesis? — egervari <ken.egervari@...> 2010/10/11

On Oct 10, 8:21m, egervari <ken.egerv...@gmail.com> wrote:

[#371610] Re: Why does a lot of code not include parenthesis? — "ara.t.howard" <ara.t.howard@...> 2010/10/12

vim completion works with, or without, the '(' and ')'. same with

[#371570] Can DRbUndumped be disabled for certain return types? — Josh Mcdade <josh.ncsu@...>

I have server model classes that definitely need DRbUndumped. Except

11 messages 2010/10/11

[#371580] more idiomatic way to avoid errors when calling method on variable that may be nil? — Charles Calvert <cbciv@...>

I'm using Ruby 1.8.7 patchlevel 249

34 messages 2010/10/11

[#371702] sort_by: multiple fields with reverse sort — Rahul Kumar <sentinel1879@...>

I need to use *sort_by* to sort a table, since the user could select

16 messages 2010/10/13

[#371704] Excel and Ruby — "Dan Sr." <djonavarro@...>

Hello all,

17 messages 2010/10/13

[#371878] Is it possible to find out if an identifier is a method alias? — Ammar Ali <ammarabuali@...>

>> def method; end

11 messages 2010/10/14
[#371880] Re: Is it possible to find out if an identifier is a method alias? — Daniel Berger <djberg96@...> 2010/10/14

On 10/14/10 4:48 PM, Ammar Ali wrote:

[#371896] Re: Is it possible to find out if an identifier is a method alias? — Ammar Ali <ammarabuali@...> 2010/10/15

On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger <djberg96@gmail.com> wrote:

[#371978] mechanize - extract href — Corey Watts <cwatts@...>

Hey there everyone. I'm having a slight problem using Mechanize. I'm

12 messages 2010/10/16

[#372016] unable to preload "rbconfig" in my irb session — "Sandeep K." <uniqueembassy@...>

I'm using Ruby 1.9.2 with Windows XP as host, I ran the command

11 messages 2010/10/17

[#372070] su {block of code.} — Guido De Rosa <guidoderosa@...>

Hi!

13 messages 2010/10/18

[#372181] Help missing something BASIC — Don Norcott <dnorcott@...>

This code is conceptually what I want to do with the nokogiri code below

11 messages 2010/10/20

[#372232] about handling args in block — salamond <jarodzz@...>

Hi, guys.

11 messages 2010/10/20

[#372234] Long conditional statements — Courtland Allen <courtlandallen@...>

Some parts of my code call for really long conditional statements of the

16 messages 2010/10/20

[#372289] generating random argument lists — Melody Class <rmiddlehouse@...>

Hi,

10 messages 2010/10/21

[#372361] Why is top-level an object rather than just Object? — John Mair <jrmair@...>

Why is it that top-level isn't just the Object class itself? what's the

14 messages 2010/10/22

[#372376] Ruby Compile. — Tridib Bandopadhyay <tridib04@...>

I am running Ruby 1.8.1 on CentOS release 4.8(Final)..

11 messages 2010/10/22

[#372493] Utilizing data from a csv file — Paul Roche <prpaulroche@...>

Hi I basically want to create a function that takes in data that has

20 messages 2010/10/24

[#372568] Can't get ruby serial port to work — Dd Dd <dd25@...>

I recently installed Ruby Version 1.9.2 on my PC. I am trying to get

14 messages 2010/10/25

[#372572] Extraction of single subarrays from multidimensional array — Maurizio Cirilli <mauricirl@...>

Hi there,

25 messages 2010/10/25

[#372652] Delete the files from mac system connected by windows. — Arihan Sinha <arihan_sinha@...>

Hi All,

10 messages 2010/10/27

[#372704] rsruby install trouble — Guybrush Threepwood <deadpool93@...>

Hello, trying for no particular reason to create a K constant calculator

15 messages 2010/10/27

[#372760] undefined method `find' for.:Module — John Hammink <john@...>

Hello,

10 messages 2010/10/28

[#372820] Is this an effective loop — Ted Flethuseo <flethuseo@...>

I was wondering if a loop of this sort would be

13 messages 2010/10/29

[#372835] Dynamically reference instance vars — Greg Willits <lists@...>

If I need to dynamically reference instance vars, is this the only way

11 messages 2010/10/30

[#372886] the dark side of inherited methods — timr <timrandg@...>

Let's say I want to make a new class, Vector (that will function,

38 messages 2010/10/31
[#372893] Re: the dark side of inherited methods — James Edward Gray II <james@...> 2010/10/31

On Oct 31, 2010, at 5:30 PM, timr wrote:

[#372951] Re: the dark side of inherited methods — Tony Arcieri <tony.arcieri@...> 2010/11/02

On Sun, Oct 31, 2010 at 5:49 PM, James Edward Gray II <james@graysoftinc.com

[#372964] Re: the dark side of inherited methods — Robert Klemme <shortcutter@...> 2010/11/02

On Tue, Nov 2, 2010 at 4:29 AM, Tony Arcieri <tony.arcieri@medioh.com> wrote:

Re: setting local variables in a binding

From: Robert Klemme <shortcutter@...>
Date: 2010-10-10 17:10:13 UTC
List: ruby-talk #371522
On 09.10.2010 08:25, John Sikora wrote:

> Robert Klemme wrote:
>> On Fri, Oct 8, 2010 at 6:06 AM, John Sikora<john.sikora@xtera.com>
>> wrote:
>>
>> So you have something like
>>
>> class Foo
>>    def self.each
>>      yield "whatever"
>>      self
>>    end
>> end
>>
>> ?  Did you also do
>>
>> class Foo
>>    def self.each
>>      yield "whatever"
>>    end
>>
>>    extend Enumerable
>> end
>>
>> ?  That would give you all methods like #find_all etc. for free.
>
> Robert,
> Note that I have kept the code and comments to a minimum here as I did
> not want the post to be even longer. I know that you are quite
> knowledgeable and that you should have little trouble following what I
> am doing - although you might not necessarily understand why I chose to
> do it this way:

Fluent speaking of Ruby makes it somewhat easier but even for me the 
length of the post needs some digesting.  So I won't come up with a full 
coverage of everything you wrote for now.  I hope my remarks are useful 
nevertheless.

> class Base
>    @class_all_enum_objects = Array.new
>
>    class<<  self
>      attr_accessor :class_all_enum_objects
>    end
>
>    def initialize
>      self.class.class_all_enum_objects<<  self
>    end
>
>    def self.enum
>       EnumeratorEOC.new(self, :each)
>    end
>
>    def self.each(&code_block)
>      self.class_all_enum_objects.each do |object|
>        yield object
>      end
>    end
> end
>
> module EnumeratorModifierEOC

This should rather be a EnumerableModifierEOC IMHO since #find is 
defined in Enumerable and this module contains all the enumerating 
methods which are based on #each (e.g. find_all, inject, map).

>    def find_all(*condition_strings,&code_block)
>       if block_given?
>         find_array = super
>       else
>         find_array = # process condition strings (syntactic sugar) here.
>       end
>       ArrayEOC.new(find_array)
>    end
>
>    # code for other methods such as sort, min, max, all?, etc. here.
> end
>
> class EnumeratorEOC<  Enumerator         # avoids modifying Enumerator.
>    include EnumeratorModifierEOC
>
>    def initilize(object, method_sym)
>      super
>    end
> end
>
> class ArrayEOC<  Array                   # avoids modifying Array.
>    include EnumeratorModifierEOC
>
>    def initialize(array = [])
>      super(array)
>    end
> end

Although no additional methods are shown (these are presented below if I 
am not mistaken) the purpose of EnumeratorEOC and ArrayEOC is mainly to 
augment standard types with additional functionality it seems.  Correct?

> To suit my purposes: 1) I want the class itself to keep track of all of
> the class instances, as shown above, 2) if an attribute has more than
> one value or is more complex than an Array, I prefer to define a class
> for it (inherited from Base) and assign attributes, 3) I want syntactic
> sugar for enumerables, 4) especially for sort, including default sort
> parameters (min and max get defaults for free as a result of the
> necessary<=>  definition). 5) I want to be able to define new functions
> and to be able to use syntactic sugar with them. See below. 6) I also
> keep track of descendants of classes although this is only partly shown
> below.  There is a little more on this at the end if anyone is still
> reading.
>
>>
>>> and I have added syntactic sugar to some of the methods. So I can be
>>> lazy (and quick) and write things like:
>>>
>>> Slot.enum.find_all(�:slot_num>= 8�, �:slot_num<= 22�, �:rate ==
>>> �ufec��).all?(�:errors == 0�)
>>
>> Is this equivalent to
>>
>> enum.find_all {|s| (8..22) === s.slot_num&&  s.rate == 'ufec'}.all?
>> {|s| e.errors == 0}
>
> Yes, you are correct (e.errors is a typo - should be s.errors), they are
> equivalent.

Sorry, that was a typo.  Thanks for catching that.

>> ?  If yes, I do not really see the advantage of your approach.  It's
>> slower needs a similar amount of typing and will detect errors later
>> (at execution time vs. at parse time).
>>>
>> If my assumption above is correct I do not understand where the "easy
>> way out" is.
>>
> I think 'sort' is the main reason for my 'easy way out'. Plus the use of
> additional functions. To illustrate, I have included in the code below
> one such function, 'sum_total_eoc' which is just 'inject' set up for
> addition, but with the ability to use syntactic sugar (of course).

In the code above I do not see any usage of #sort so I don't really see 
the easy way out in the example above.  Even if you would need the 
String argument eval solution for sorting it would not be necessary for 
the code above which uses #find_all and #all?.

> You may or may not agree with my 'easy way out', but here goes:
>
> First, a short explanation: I have attribute and expression comparables
> for each class. The comparables are compared in the order that they are
> given in attr_accessor. There is also a class method called
> set_comparables_order that allows the user to re-arrange the order if
> desired (it also allows expressions, not just attributes). Child classes
> inherit the comparables and their order from the parent class. Any new
> attributes defined in attr_accessor in the child class are added to the
> comparables up front.

A problem of your design is that you change the classes idea of default 
ordering.  What I mean is this: you need to modify a class to achieve a 
particular ordering.  Now the standard behavior of sorting has changed 
and you can never tell what ordering you will get by only looking at a 
particular piece of code which only contains the call to #sort.

IMHO it would be better to not allow this but instead create a mechanism 
which makes the ordering explicit.  Here is one approach

module EnumeratorModifierEOC # or Enumerable
   def sort_by_fields(*fields)
     sort_by {|x| fields.map {|f| x.send(f)}}
   end
end

This might not be the most efficient solution because of the repeated 
invocation of #map.  Alternatively you can easily stick with #sort_by by 
doing

enum.sort_by {|x| [x.field_1, x.field_2]}

which is pretty short, readable and efficient.

You could improve this by creating something like a Sorter:

def Sorter(*fields)
   eval "lambda {|enum| enum.sort_by{|x| [#{fields.map {|f| 
"x.#{f}"}.join(', ')}]}}"
end

MAIN = Sorter(:foo, :bar)
...
sorted = MAIN[enum]

Now you actually have your eval but you use it to compile code which you 
can then use more often efficiently. :-)

You can even make this more modular by removing the sorting and 
compiling only the field extraction:

def FieldExtractor(*field)
   eval "lambda {|x| [#{fields.map {|f| "x.#{f}"}.join(', ')}]}"
end

MAIN = FieldExtractor(:foo, :bar)
...
sorted = enum.sort_by &MAIN

> OK, so what can I do with this? The following are some simplified
> examples:
>
> class Slot<  EnumerableObjectClass::Base
>    attr_accessor :slot_num, :rate, :error_info #<--Array of ErrorInfo
> objects.
>
>    def initialize(slot_num, rate, error_info)
>      super()
>      @slot_num, @rate, @error_info = slot_num, rate, error_info
>    end
> end
>
> class ErrorInfo<  EnumerableObjectClass::Base
>    attr_accessor :time_stamp, :num_errors
>
>    def initialize(time_stamp, num_errors)
>      super()
>      @time_stamp, @num_errors = time_stamp, num_errors
>    end
> end
>
> Slot.new(3, 'efec', [ErrorInfo.new('10-07-10', 500),
> ErrorInfo.new('10-08-10', 1000)])
> Slot.new(1, 'ufec', [ErrorInfo.new('10-04-10', 1000),
> ErrorInfo.new('10-05-10', 2000)])
> Slot.new(8, 'ufec', [ErrorInfo.new('10-07-10', 1500),
> ErrorInfo.new('10-08-10', 1200)])
> Slot.new(4, 'efec', [ErrorInfo.new('10-07-10', 3500)])
> Slot.enum{}.find(':slot_num == 4').error_info<<
> ErrorInfo.new('10-08-10', 2500)
>
> -------------
> # note - most of the examples below do not use variables. However, this
> is not a limitation and a couple of examples are given that use
> variables.
>
> # default sort is sorted by :slot_num, etc. - order of attr_accessor
> p Slot.enum.sort.collect(:slot_num)
> # =>  [[1], [3], [4], [8]]
>
> # sort by total errors.
> p
> Slot.enum.sort(':error_info.sum_total_eoc(:num_errors)').collect(:rate,
> ':error_info.sum_total_eoc(:num_errors)')
> # =>  [["efec", 1500], ["ufec", 2700], ["ufec", 3000], ["efec", 6000]]
>
> # sort by rate, total errors.
> p Slot.enum.sort(:rate,
> ':error_info.sum_total_eoc(:num_errors)').collect(:rate,
> ':error_info.sum_total_eoc(:num_errors)')
> # =>  [["efec", 1500], ["efec", 6000], ["ufec", 2700], ["ufec", 3000]]
>
> # sort by rate, total errors, with total errors sorted high to low by
> ending with '___' triple underscore.
> p Slot.enum.sort(:rate,
> ':error_info.sum_total_eoc(:num_errors)___').collect(:rate,
> ':error_info.sum_total_eoc(:num_errors)', :slot_num)
> # =>  [["efec", 6000, 4], ["efec", 1500, 3], ["ufec", 3000, 1], ["ufec",
> 2700,8]]
>
> I think that the above would be hard to do in a one-liner w/o syntactic
> sugar, so this is my 'easy way out' (although I would not be surprised
> if you came up with one Robert). I think this is pretty easy and it
> saves me time.

The tricky thing I did not consider yet is that you have nested access 
to fields and not just one level.  In my world this would look like

p Slot.enum.sort_by {|x| [x.rate, 
x.error_info.sum_total_eoc(:num_errors)___]}.
map {|x| [x.:rate, x.error_info.sum_total_eoc(:num_errors), x.slot_num]}

That's certainly not nice to squeeze on one line but your solution does 
not look that much shorter.

> # total errors of all slots.
> p Slot.enum.sum_total_eoc(':error_info.sum_total_eoc(:num_errors)')
>   # =>  13200
>
> # slots that have>= 3000 total errors.
> p Slot.enum.find_all(':error_info.sum_total_eoc(:num_errors)>=
> 3000').collect(:slot_num)
> # =>  [[1], [4]]
>
> # use a variable for sort parameter.
> var = ':slot_num'
> p Slot.enum.sort("#{var}").collect("#{var}")
> # =>  [[1], [3], [4], [8]]
>
> # use a variable w/o interpolation - must pass a binding with enum{}
> (which is where this topic started by the way). It is carried along as
> an attribute to ArrayEOC and/or EnumeratorEOC, similar to coparables.
> var = ':slot_num'
> p Slot.enum{}.sort(var).collect(var)
> # =>  [[1], [3], [4], [8]]
>
> # slots that have>= 2500 errors in a single time period.
> p Slot.enum.find_all(":error_info.any?(:num_errors>=
> 2500)").collect(:slot_num)
> # =>  Exceptions galore! my simple 'parser' breaks down. will have to use
> the 'old fashioned way' of a code block. maybe I will try to get this to
> work in the future.
>
> In case anyone is wondering, if data is input as an Array, it comes out
> as an ArrayEOC the first time it is read. In the simplified code above,
> I show a call to super in attr_accessor, but really for the reader part,
> I add a line that converts an Array to an ArrayEOC. I do this so I can
> use the ArrayEOC methods as mofified since I want to keep Array's
> methods unmodified.
>
> Since I already use self.inherited, I am able to keep track of
> descendants (a recursive jaunt through the child classes using inject to
> build an array), so there is enum_only{} which enumerates only the given
> class, and enum{} which enumerates the given class and all descendants.
> I do this because I have a number of units of similar type which follow
> the classic inheritance OO model (even though it is apparent from posts
> that this has fallen out of favor). Oh well, it seems that I am behind
> the times on a lot of things.
>
> Oh, and since Ruby is dynamic, I had to modify remove_method and
> undefine_method to remove any comparables, if applicable (there is a
> difference between the two however). And if there are subsequent calls
> to attr_accessor, I have to add those methods to the class comparables
> in the class and any descendant classes. I allow the user to reset the
> comparables to those given in the class and its ancestors. I also tried
> to make the code reflective, by making the comparables, child_classes,
> descndants, etc. availble.

Your need to redefine remove_method etc. is fallout of your design 
decision to change the default ordering.  As I said, I believe there are 
better and more efficient designs.

> Writing this code has certainly been fun and
> educational!

That's good!

There are still some things that I didn't yet wrap my head around: why 
do you want to make classes keep track of all their instances?  This 
essentially makes classes global variables - but less obvious.  During 
the course of a program usually you create instances and let forget them 
again.  But in your situation you will keep all instances around and 
there is no clear demarcation.  If you want automated tracking of 
instances there are other options, e.g.

class InstanceTracker
   def initialize
     @all = Hash.new {|h,k| h[k] = []}
   end

   def new(cl,*args, &b)
     @all[cl] << cl.new(*args, &b)
   end

   def clear
     @all.clear
     self
   end

   def clear_class(cl)
     @all.delete(cl)
     self
   end

   include Enumerable

   def each(&b)
     if b
       @all.each {|k,v| v.each(&b)}
       self
     else
       Enumerator.new(self, :each)
     end
   end

   def each_class(cl, &b)
     if b
       @all[cl].each(&b)
       self
     else
       Enumerator.new(self, :each_class, cl)
     end
   end

   def each_sub_classes(cl, &b)
     if b
       @all.each {|k,v| v.each(&b) if k <= cl}
       self
     else
       Enumerator.new(self, :each_sub_classes, cl)
     end
   end
end

it = InstanceTracker.new

it.new(Foo, 1, 2)
it.each_class(Foo).find_all {|f| f.size > 0}

etc.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


In This Thread