Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class method aliasing

0 views
Skip to first unread message

Bob Hutchison

unread,
Oct 17, 2005, 9:15:52 PM10/17/05
to
Hi,

I'd like to confirm that I cannot use any kind of aliasing to tidy up
the following situation:

class Junk
def Junk.info
"blah blah blah"
end

def info
Junk.info
end
end

I'd really like to be able to write the following:

puts Junk.info

# then somewhere far from there...
junk = Junk.new
puts junk.info

I don't find junk.class.info satisfying, and if that were a module
method, then you have to know too much.

I guess what I'm trying to do is make class/module methods directly
available to instances, and it seems I can't do that (whatever the
reason).

Any better ideas are more than welcome.

Cheers,
Bob


----
Bob Hutchison -- blogs at <http://www.recursive.ca/hutch/>
Recursive Design Inc. -- <http://www.recursive.ca/>
Raconteur -- <http://www.raconteur.info/>


Ara.T.Howard

unread,
Oct 17, 2005, 9:34:09 PM10/17/05
to
On Tue, 18 Oct 2005, Bob Hutchison wrote:

> Hi,
>
> I'd like to confirm that I cannot use any kind of aliasing to tidy up the
> following situation:
>
> class Junk
> def Junk.info
> "blah blah blah"
> end
>
> def info
> Junk.info
> end
> end
>
> I'd really like to be able to write the following:
>
> puts Junk.info
>
> # then somewhere far from there...
> junk = Junk.new
> puts junk.info
>
> I don't find junk.class.info satisfying, and if that were a module method,
> then you have to know too much.
>
> I guess what I'm trying to do is make class/module methods directly available
> to instances, and it seems I can't do that (whatever the reason).
>
> Any better ideas are more than welcome.
>
> Cheers,
> Bob

harp:~ > cat a.rb
class Module
def mod_attr a
module_eval <<-code
def #{ a }
self::class.send '#{ a }'
end
def #{ a }= value
self::class.send '#{ a }=', value
end
code
end
end

class C
def C::info
42
end
mod_attr 'info'
end

p C::info

p C::new.info


harp:~ > ruby a.rb
42
42


-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================

Sean O'Halpin

unread,
Oct 17, 2005, 9:38:30 PM10/17/05
to
On 10/18/05, Bob Hutchison <hu...@recursive.ca> wrote:
> Hi,
>
> I'd like to confirm that I cannot use any kind of aliasing to tidy up
> the following situation:

[snip example - see below]

> I guess what I'm trying to do is make class/module methods directly
> available to instances, and it seems I can't do that (whatever the
> reason).
>
> Any better ideas are more than welcome.
>
> Cheers,
> Bob

How about this?

class Class
def class_function(name)
class_eval { define_method(name, &method(name)) }
end
end

class Junk
def Junk.info
"blah blah blah from #{self}"
end

class_function :info
end

puts Junk.info


junk = Junk.new
puts junk.info

__END__
blah blah blah from Junk
blah blah blah from Junk

Regards,

Sean


Sean O'Halpin

unread,
Oct 17, 2005, 9:44:55 PM10/17/05
to
Though maybe class_function isn't the right name. Perhaps
instance_function or object_function? (All on analogy with
module_function). Also, it might be handy to do this:

class Class
def instance_function(*names)
names.each do |name|


class_eval { define_method(name, &method(name)) }
end
end

end

so you can do this:

instance_function :info, :hello

Regards,

Sean


Robert Klemme

unread,
Oct 18, 2005, 3:36:53 AM10/18/05
to
Bob Hutchison wrote:
> Hi,
>
> I'd like to confirm that I cannot use any kind of aliasing to tidy up
> the following situation:
>
> class Junk
> def Junk.info
> "blah blah blah"
> end
>
> def info
> Junk.info
> end
> end
>
> I'd really like to be able to write the following:
>
> puts Junk.info
>
> # then somewhere far from there...
> junk = Junk.new
> puts junk.info
>
> I don't find junk.class.info satisfying, and if that were a module
> method, then you have to know too much.
>
> I guess what I'm trying to do is make class/module methods directly
> available to instances, and it seems I can't do that (whatever the
> reason).
>
> Any better ideas are more than welcome.

Here's another approach:

module Helper
def info() 42 end
end
class Foo
include Helper
extend Helper
end

>> Foo.info
=> 42
>> Foo.new.info
=> 42

Kind regards

robert

Bob Hutchison

unread,
Oct 18, 2005, 8:42:37 AM10/18/05
to
Thanks! I think a variation of Ara's and Sean's suggestion with
your's Robert cover the various circumstances I've got to deal with.

Cheers,
Bob

----

ES

unread,
Oct 18, 2005, 2:33:12 PM10/18/05
to
Bob Hutchison wrote:
> Thanks! I think a variation of Ara's and Sean's suggestion with your's
> Robert cover the various circumstances I've got to deal with.

You can automate things a bit (thanks to bitsweat for helping me find
singleton_method_added again). Here is a version which enables you to
easily control which classes have this feature:


module ParentalControls
module ClassIncludes
def singleton_method_added(sym)
class_eval <<-END
def #{sym}(*a, &b)
self.class.#{sym}(*a, &b)
end
END
end
end

def self.included(mod)
mod.extend ClassIncludes
end
end

class Foo
include ParentalControls

def self.foo()
42
end
end

puts Foo.foo
puts Foo.new.foo

E

0 new messages