php's __call for ruby? or better approach?

Hi Rubyists!

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

-------- (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.

Well you'll have to pick different names because 1_1 isn't a legal
method name

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]; }

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

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.

LOL, send method does the job :slight_smile:

I didn't think it would be that simple!