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)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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> 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> 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) > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> 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 > > > 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)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
LOL, send method does the job :) I didn''t think it would be that simple! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---