Forum: Ruby Record good idea?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7010c2a9fa9ebf326c7c858cd656926b?d=identicon&s=25 Frank Hirsch (0ffh)
on 2006-05-06 11:08
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 Hirsch 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
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-05-06 12:09
(Received via mailing list)
On Sat, 2006-05-06 at 18:08 +0900, Frank Hirsch 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-06 12:12
(Received via mailing list)
2006/5/6, Frank Hirsch <0ffh@frankhirsch.net>:
> 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
=> #<OpenStruct>
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
7010c2a9fa9ebf326c7c858cd656926b?d=identicon&s=25 Frank Hirsch (0ffh)
on 2006-05-06 12:48
Ross Bamford 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... ;-)

> But please also check out:
> 	http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/
> 	http://www.ruby-doc.org/core/classes/Struct.html

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!
18ca239ffade6df0b839d26062f173fb?d=identicon&s=25 Dominik Bathon (Guest)
on 2006-05-06 18:49
(Received via mailing list)
Hi Ross,

On Sat, 06 May 2006 12:06:32 +0200, Ross Bamford
<rossrt@roscopeco.co.uk>
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
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-05-06 20:09
(Received via mailing list)
On Sun, 2006-05-07 at 01:46 +0900, Dominik Bathon 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 :)
This topic is locked and can not be replied to.