# Record good idea?

**URL:** <https://www.ruby-forum.com/t/record-good-idea/94540>\
**Category:** Ruby\
**Created:** [May 6, 2006, 11:08am UTC](https://www.ruby-forum.com/t/record-good-idea/94540 "2006-05-06T11:08:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 11:08am UTC](https://www.ruby-forum.com/t/record-good-idea/94540/1 "2006-05-06T11:08:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/record-good-idea/94540/2 "2006-05-06T12:09:48Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/record-good-idea/94540/3 "2006-05-06T12:12:55Z")

</div>

2006/5/6, Frank H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/record-good-idea/94540/4 "2006-05-06T12:48:14Z")

</div>

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… 😉

> But please also check out:  
> [http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/)  
> [class Struct - RDoc Documentation](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!

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 8:09pm UTC](https://www.ruby-forum.com/t/record-good-idea/94540/5 "2006-05-06T20:09:05Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/record-good-idea/94540/6 "2006-05-06T18:49:35Z")

</div>

Hi Ross,

On Sat, 06 May 2006 12:06:32 +0200, Ross B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
