Record good idea?

Hija!

I have made a little record class, because I needed it.

Maybe anyone finds this useful, or wants to beat me,
because I have overtreaden some unwritten Ruby rule… :-)))

---------------- cut here ----------------<

class Record
=begin
Little record class.
(w) Frank H. 2006
=end
def method_missing(*a)
if (a.length!=2)
super
elsif (a[0].class!=Symbol)
super
else
name=a[0].id2name
value=a[1]
eval("@"+name+“value”,binding)
eval(“def “+name+”(v);@”+name+“v;end”)
name.chop!
eval(“def “+name+”;@”+name+";end")
end
end
def initialize
end
end

---------------- and here ----------------<

Okay, what does anybody think?
Useful or heresy or just an old hat?

Regards, 0ffh

On Sat, 2006-05-06 at 18:08 +0900, Frank H. wrote:

=begin
value=a[1]

---------------- and here ----------------<

Okay, what does anybody think?
Useful or heresy or just an old hat?

I took the liberty of refactoring your code a bit, mainly to avoid
string eval and to ignore non-attribute single-arg methods:

class Record
def method_missing(sym, *a)
if (name = sym.to_s) =~ /[^=]=$/
rdr, ivar, value = name.chop, “@#{rdr}”, a.first

  (class << self; self; end).class_eval do
    define_method(sym) { |v| instance_variable_set("@#{rdr}",v) }
    define_method(rdr) { instance_variable_get("@#{rdr}") }
  end

  self.send(sym,value)
else
  super
end

end
end

But please also check out:

http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/
http://www.ruby-doc.org/core/classes/Struct.html

2006/5/6, Frank H. [email protected]:

class Record
name=a[0].id2name

---------------- and here ----------------<

Okay, what does anybody think?
Useful or heresy or just an old hat?

This looks like another implementation of OpenStruct…

irb(main):027:0> require ‘ostruct’
=> true
irb(main):028:0> rec = OpenStruct.new
=> #
irb(main):030:0> rec.name
=> nil
irb(main):031:0> rec.name=“foo”
=> “foo”
irb(main):032:0> rec.name
=> “foo”

Kind regards

robert

Ross B. wrote:

I took the liberty of refactoring your code a bit, mainly to avoid
string eval and to ignore non-attribute single-arg methods:

[code omitted]

Weeeellll, seems once again TIMTOWTDI…
only your way I do not understand yet (new 2 Ruby).
It will be fun to find out what exactly you are doing there… :wink:

But please also check out:
http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/
class Struct - RDoc Documentation

So I guess the idea was not so bad, but also plain obvious.
Well…

Regards, 0ffh


And once again the killer coding ninja monkeys reinvented the wheel!

On Sun, 2006-05-07 at 01:46 +0900, Dominik B. wrote:

This doesn’t work as you think, Ruby first creates an array of the values
on the right hand side and then assigns it to the variables on the left:

Oops. Something was bothering me when I did that, now I know what…
Thanks :slight_smile:

Hi Ross,

On Sat, 06 May 2006 12:06:32 +0200, Ross B.
[email protected]
wrote:

class Record
def method_missing(sym, *a)
if (name = sym.to_s) =~ /[^=]=$/
rdr, ivar, value = name.chop, “@#{rdr}”, a.first

This doesn’t work as you think, Ruby first creates an array of the
values
on the right hand side and then assigns it to the variables on the left:

irb(main):001:0> a, b, c = 1, “@#{a}”, a
=> [1, “@”, nil]
irb(main):002:0> [a, b, c]
=> [1, “@”, nil]

Dominik