Thank you all for your responses, but I can see I didn''t make myself clear. I know that Ruby doesn’t work the way I want it to, and I know why it works the way it does. I am completely aware of the difference between a class and an object. This is just a dream for how I wish Ruby did work, it would make some of the programming for the Rails framework much easier and elegant. Ulysses addressed my issue closest by talking about append_features. I would also love to be able to do this in Ruby: class Test x = true if x def foo(bar) @wibble = bar end end for i in 1..10 def ok(me) @hello[i] = me end end ok “you” end -Lucas http://rufy.com/
On Sun, 27 Mar 2005 12:13:15 -0800, Lucas Carlson <lucas-1eRuzFDw/cg@public.gmane.org> wrote:> This is just a dream for how I wish Ruby did work, it would make some > of the programming for the Rails framework much easier and elegant. > Ulysses addressed my issue closest by talking about append_features. > > I would also love to be able to do this in Ruby: > > class Test > x = true > if x > def foo(bar) > @wibble = bar > end > endConditional definition of methods? Then if you call the method and it''s not there, you get a NoMethodError. What is wrong with this? : @allow_foo = true def foo(bar) return nil if !@allow_foo @wibble = bar end> for i in 1..10 > def ok(me) > @hello[i] = me > end > end > > ok "you" > endThis part doesn''t even make sense. You''re trying to define 10 functions with the same name? When you call the method, how do you know which one you are calling? def ok(me) for i in 1..10 @hello[i] = me end end -- Urban Artography http://artography.ath.cx
On 28/03/2005, at 6:13 AM, Lucas Carlson wrote:> I would also love to be able to do this in Ruby: > > class Test > > for i in 1..10 > def ok(me) > @hello[i] = me > end > endfor i in 1..10 define_method("action#{i}") do # do something end end And a more practical example, this Heiraki code snippet: for action, list_method in {:up => :move_higher, :down => :move_lower, :top => :move_to_top, :bottom => :move_to_bottom} do define_method(action) do if @params[''id''] @chapter = Chapter.find(@params[''id'']) @chapter.send(list_method) end redirect_to_chapter @chapter end end - tim lucas
Rob Park wrote:> Conditional definition of methods? Then if you call the method and > it''s not there, you get a NoMethodError. What is wrong with this? : > > @allow_foo = true > def foo(bar) > return nil if !@allow_foo > @wibble = bar > endThis will not work, unfortunately. Use this: def self.allow_foo() true end def foo(bar) return nil unless self.class.allow_foo @wibble = bar end