# Php's \_\_call for ruby? or better approach?

**URL:** <https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467>\
**Category:** Rails\
**Created:** [December 28, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467 "2008-12-28T14:05:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero0x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c6cbf5/32.png) [@zero0x](https://www.ruby-forum.com/u/zero0x)\
**Post date:** [December 28, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467/1 "2008-12-28T14:05:11Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![zero0x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c6cbf5/32.png) [@zero0x](https://www.ruby-forum.com/u/zero0x)\
**Post date:** [December 28, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467/2 "2008-12-28T14:25:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zero0x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c6cbf5/32.png) [@zero0x](https://www.ruby-forum.com/u/zero0x)\
**Post date:** [December 28, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467/3 "2008-12-28T14:28:05Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zero0x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c6cbf5/32.png) [@zero0x](https://www.ruby-forum.com/u/zero0x)\
**Post date:** [December 28, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/phps-call-for-ruby-or-better-approach/156467/4 "2008-12-28T14:43:56Z")

</div>

LOL, send method does the job 🙂

I didn’t think it would be that simple!
