Forum: Ruby on Rails php's __call for ruby? or better approach?

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.
79ad5a1770c5cf086735a10ef7db6598?d=identicon&s=25 zero0x (Guest)
on 2008-12-28 14:05
(Received via mailing list)
Hi Rubyists!

I'm quite new to Ruby/Rails thing, so please be patient with me :)

-------- (skip to next -------- if you want to quickly see the
problem)

I am learning by creating something like timetable application (like
in school). There are days 1-5 and lessons 1-8.

I have to db tables, subjects, which is the list of subjects, and
subject_lessons, which is assocation of a subject to a particular day
and lesson. It's a one-to-many relationship, because one subject can
be assigned to more lessons, but on one lesson there could be only one
subject.

Now I've been playing with select and collection_select, and I found
out, that I can set value by specifying an object and a method. Pretty
cool for static tables, but this is kinda more dynamic. I have loops
to generate a table, and each lesson should have a select box.

--------

So I created a new class, called timetable, and I would like to create
methods like 1_1, 1_1=, 1_2, 1_2= - getters and setters for each
lesson of each day.

By the way, the timetable has a result set from active record as a
private variable, and I turned it into hash, so that I could access it
by @subject_lessons['1_1'] etc.

In PHP, there's a __call method, which would do something like this:

function __call($function, $arguments)
{
  $this->subject_lessons[$function] = $arguments[0];
}

So I need something like this in Ruby. Hope someone will help me :)
Thank you guys very much! :)

---

By the way Ruby is beautiful but it is pretty different from PHP (I'm
working in PHP for like 4 years, and in Ruby for like few days)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-28 14:25
(Received via mailing list)
On 28 Dec 2008, at 13:04, zero0x wrote:

>
> --------
>
> So I created a new class, called timetable, and I would like to create
> methods like 1_1, 1_1=, 1_2, 1_2= - getters and setters for each
> lesson of each day.
>
Well you'll have to pick different names because 1_1 isn't a legal
method name
>
Still not entirely clear to me what this does, but I'd be surprised if
some combination of method_missing and send didn't get you there.

Fred
79ad5a1770c5cf086735a10ef7db6598?d=identicon&s=25 zero0x (Guest)
on 2008-12-28 14:28
(Received via mailing list)
It is magic method. It is called when any method is called with the
called method name as the first argument and array of arguments as the
second argument.

Mentioned method_missing looks like exactly what I need so I'm going
to do a google search on it.

On 28. Dec., 14:24 h., Frederick Cheung <frederick.che...@gmail.com>
79ad5a1770c5cf086735a10ef7db6598?d=identicon&s=25 zero0x (Guest)
on 2008-12-28 14:43
(Received via mailing list)
LOL, send method does the job :)

I didn't think it would be that simple!
This topic is locked and can not be replied to.