Little Things

Hi,

In message “Re: Little Things”
on Mon, 1 Jan 2007 05:42:23 +0900, Devin M.
[email protected] writes:

|I take it you don’t like meta_class, the most often proposed name for
|it. How about virtual_class, or singleton_class, or both?

It’s not really a meta class since it’s not only for classes,
nor virtual_class since it’s not virtual anymore.

In my mindset, two candidates have survived. singleton_class (or
singletonclass), which can easily be confused with a class in the
singleton design pattern. And eigenclass, which is unfamiliar for
most people.

						matz.

Dave T. wrote:

obj.implementation
That’s an great perspective. Perhaps even different enough to get us
out of this quagmire.

Seems to me that 80% of the time we want to use the “singleton class”
with class_eval. Expressions like “singleton_class.class_eval” are
rather redundant and long winded (IMHO). So maybe another single term
for this, along the lines of what you’re saying, would be better.
Looking at Phrogz’ wiki page of names I came up with:

customize do

end

Then the class could be called the ‘customization’. Ie. the above being
equvalent to:

customization.class_eval do

end

T.

Hi,

In message “Re: Little Things”
on Mon, 1 Jan 2007 22:31:06 +0900, [email protected] writes:

|I don’t think the “singleton” confusion is a big problem. I’ve heard
|many people say that it might be confusing, but very few people
|actually be confused by it. And if they are, it’s worth the three
|seconds it takes them to learn the difference :slight_smile:
|
|Also, unless we’re supposed to start using the term “eigenmethod” for
|singleton methods, singleton_class is a better match in that sense.

Makes sense. Eigenmethods, hmm.

						matz.

Dave T. wrote:

I wonder: is it a mistake to think of it as a class at all?
I don’t think so, since class << Object.new; self end.is_a? Class. That
said, I’m not against names that don’t involve the word ‘class’.

Then the class could be called the ‘customization’.
Ooh… I like that.

Then all we need is a sentence in ri Class that reads ‘Classes inherit
the customizations of their superclasses.’ :slight_smile:

Seriously. Rolls off the tongue. Simple. Descriptive. Plenty of
opportunities for variation – plenty of ways to work into English
sentences.

In short: +1

On 1/1/07, Trans [email protected] wrote:

...

end

That sure looks pretty. I do think maybe it’d be confusing to
remember where the code in the block to customize() is actually being
evaluated, but this could be a decent alternative.

Then again, I don’t have any beef at all with singleton class/method
OR eigen class/method

I’d just like to see an end to (class << self; self; end) and
preferably avoid seeing ‘meta’ become part of method names, lest we
begin the meta_meta and the meta_meta_meta :wink:

On 12/30/06, Trans [email protected] wrote:

I was a bit surprised about Matz mention of the little things in his
last ketynote.
Little things can make all the difference! In fact, long time Rubyists
have been waiting a long for some important “little” things. Here’s
some of the things on my little list…
[…]

When we’re talking about little things, I’ve got one suggestion as
well. Python has the nice trick that you can call any object like a
function by simply overriding the call method. Something like that
would be nice to have in Ruby as well. When you call an object with
the normal syntax, the fallback could be the #call method or some
such. Then it’d be possible to do this:

def some_method(&proc)
proc(“Hello”)
proc “Hello”
proc 1, 2, 3
end

I admit that having a separate function namespace means that we can
have ice syntax for method calls with no parens, but something like
this would permit some cool tricks.

On 1/1/07, Dave T. [email protected] wrote:

as it’s the execution environment of an object. So, just to get the

Dave

I would like to put my vote in for

obj.super_ego

On Tue, Jan 02, 2007 at 12:04:35AM +0900, Trans wrote:
} Dave T. wrote:
} > I wonder: is it a mistake to think of it as a class at all? Sure, it
} > had a ‘class’ somewhere in its background, but is potentially moved
} > on a lot since then. In a way, it’s move analogous to a
meta-binding,
} > as it’s the execution environment of an object. So, just to get the
} > creative juices flowing, and in the spirit of brainstorming, how
about
[…]

The only way in which a class and a module differ is that you can create
an
instance of a class. Current Ruby 1.8.5 will not let you call #new on a
singleton class, so one could argue that it’s more a module than a
class,
remembering that Class subclasses Module. It is,
however, at least a module. It is very important that it can be treated
as
a module, i.e. responds to various methods defined in Module. I’d claim
that #include and #define_method are the two most important of those.

} Seems to me that 80% of the time we want to use the “singleton class”
} with class_eval. Expressions like “singleton_class.class_eval” are
} rather redundant and long winded (IMHO). So maybe another single term
} for this, along the lines of what you’re saying, would be better.
} Looking at Phrogz’ wiki page of names I came up with:
}
} customize do
} …
} end
}
} Then the class could be called the ‘customization’. Ie. the above
being
} equvalent to:
}
} customization.class_eval do
} …
} end

If I hadn’t had occasion to do significant metaprogramming and was
looking
at this, I’d agree with you that this is nice. As it is, there are
really
only four things I do to interact with the singleton class, in order of
frequency of use:

  1. include a module
  2. define a method
  3. alias a method
  4. remove a method

Now, #1 is served by Object#extend and doesn’t require explicit access
to
the singleton class at all. With #2, 99% of the time I use
#define_method,
and the only time I don’t is when I need to define a block-accepting
method. (Actually, I use send(:define_method, …) because
#define_method
is private. Could we make #define_method public for all singleton
classes,
please? How about a SingletonClass subclass of Class, or possibly
module,
that makes it public?) I actually need #3 and #4 vanishingly rarely, and
most of the time it makes more sense to do it in a module anyway which
can
be included with extend.

I just realized I forgot attr_accessor and friends, but I’d be inclined
to
use #send (or #funcall) with that as well. What I’m saying, though, is
that
if I had #customize and #customization (and #define_method were public
for
singleton classes), I’d still do something like this (note: contrived
example):

def setup_hashed_attributes(obj, hash)
if attrs = obj.instance_variable_get(:@hashed_attributes)
attrs.merge(hash)
else
obj.instance_variable_set(:@hashed_attributes, hash.dup)
end
singleton = obj.customization
singleton.send(:attr_reader, :hashed_attributes)
hash.keys.each { |k|
singleton.define_method(k) { @hashed_attributes[k] }
singleton.define_method("#{k}=") { |v| @hashed_attributes[k] = v }
}
end

…rather than the equivalent…

def setup_hashed_attributes(obj, hash)
if attrs = obj.instance_variable_get(:@hashed_attributes)
attrs.merge(hash)
else
obj.instance_variable_set(:@hashed_attributes, hash.dup)
end
obj.customize {
attr_reader :hashed_attributes
hash.keys.each { |k|
define_method(k) { @hashed_attributes[k] }
define_method("#{k}=") { |v| @hashed_attributes[k] = v }
}
}
end

I just like how the first one reads. The first one says, “I am messing
with
an object’s capabilities by poking at its instance variables and adding
methods to it.” The second one says, “I am reopening a class and adding
methods to it after poking at an instance variable.”

} T.
–Greg

Gregory S. wrote:

The only way in which a class and a module differ is that you can create an
instance of a class. Current Ruby 1.8.5 will not let you call #new on a
singleton class, so one could argue that it’s more a module than a class,
remembering that Class subclasses Module. It is,
however, at least a module. It is very important that it can be treated as
a module, i.e. responds to various methods defined in Module. I’d claim
that #include and #define_method are the two most important of those.

Boy, I wish it were a module. That would make something a hek of a lot
easier. Personally I never cared for the distiction between Class and
Module. I feel it is rather arbitrary and I spend too much time wonder
which one I should use for a give case.

Now, #1 is served by Object#extend and doesn’t require explicit access to
the singleton class at all. With #2, 99% of the time I use #define_method,
and the only time I don’t is when I need to define a block-accepting
method. (Actually, I use send(:define_method, …) because #define_method
is private. Could we make #define_method public for all singleton classes,
please? How about a SingletonClass subclass of Class, or possibly module,
that makes it public?) I actually need #3 and #4 vanishingly rarely, and
most of the time it makes more sense to do it in a module anyway which can
be included with extend.

I’d put #2 before #1 and often ‘def obj.meth’ serves the purpose.

obj.instance_variable_set(:@hashed_attributes, hash.dup)

end
singleton = obj.customization
singleton.send(:attr_reader, :hashed_attributes)
hash.keys.each { |k|
singleton.define_method(k) { @hashed_attributes[k] }
singleton.define_method("#{k}=") { |v| @hashed_attributes[k] = v }
}
end

I don’t know. If you’re going to go to all that trouble why bother with
the assignment? Just do:

def setup_hashed_attributes(obj, hash)
if attrs = obj.instance_variable_get(:@hashed_attributes)
attrs.merge(hash)
else
obj.instance_variable_set(:@hashed_attributes, hash.dup)
end
obj.customization.send(:attr_reader, :hashed_attributes)
hash.keys.each { |k|
obj.customization.define_method(k) { @hashed_attributes[k] }
obj.customization.define_method("#{k}=") { |v|
@hashed_attributes[k] = v }
}
end

That reads better IMO. You could alwasy add #define_custom_method too
of course.

T.

On Jan 1, 2007, at 4:36 PM, Gregory S. wrote:

The only way in which a class and a module differ is that you can
create an
instance of a class.

Well that and the fact that class objects are organized in a tree via
inheritance. Modules are not.

Gary W.

On 2006/12/31, at 07:38, Rob S. wrote:

obj.send(:foo) # will call only public
obj.send!(:foo) # will bypass private/protected, like the current
send.

+1

On Thu, 4 Jan 2007, [ISO-8859-1] Paulo Köch wrote:

On 2006/12/31, at 07:38, Rob S. wrote:

obj.send(:foo) # will call only public
obj.send!(:foo) # will bypass private/protected, like the current send.

+1

++

-a

On 1/3/07, [email protected] [email protected] wrote:

-a

if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. -
the
dalai lama

It is not frequent that I dare not do agree with Gurus like Ara (1) but
I
have a very strong feeling that the ! suffix should indicate an object
inplace change instead of the object not being touched, as e.g. in sub
vs.
sub! (2) etc.
I feel it is not a good idea to define send! as mentioned above.

Cheers
Robert

(1) See James I can define the experts :wink: just kidding the expert thingy
is
dangerous stuff I agree fully!!!
(2) OT but still interesting I feel it might be a good convention that
any
method changing the object should have a ! suffix, would make
programs much more readable, don’t you think?

R.

“The real romance is out ahead and yet to come. The computer revolution
hasn’t started yet. Don’t be misled by the enormous flow of money into
bad
defacto standards for unsophisticated buyers using poor adaptations of
incomplete ideas.”

  • Alan Kay

[email protected] wrote:

On Thu, 4 Jan 2007, [ISO-8859-1] Paulo Köch wrote:

On 2006/12/31, at 07:38, Rob S. wrote:

obj.send(:foo) # will call only public
obj.send!(:foo) # will bypass private/protected, like the current send.

+1

++

Come on guys. Has anyone read my critique? You can “plus one” til the
cow’s come home, but you totally ignore the issues. Show me you know
something about the issue, tell me why. Otherwise your +1’s are
littel more than cooy feelings.

Do you understand that #send is tantamount to a keyword?

T.

Hi –

On Mon, 1 Jan 2007, Trans wrote:

–I made attempts at emailing on the second --and I sumbitted the first
:wink:

Can you tell me what happened when you tried the email? Did you
subscribe to it at the site? If something’s wrong, I can’t fix it
unless I hear about it. If people are just not using the site much,
that’s not the same as its being broken.

David

Hi –

On Thu, 4 Jan 2007, Robert D. wrote:

(2) OT but still interesting I feel it might be a good convention that any
method changing the object should have a ! suffix, would make
programs much more readable, don’t you think?

No. I would not want to see pop turned into pop! or << turned into
<<! and so forth.

David

On Thu, 4 Jan 2007, Trans wrote:

++

Come on guys. Has anyone read my critique? You can “plus one” til the
cow’s come home, but you totally ignore the issues. Show me you know
something about the issue, tell me why. Otherwise your +1’s are
littel more than cooy feelings.

Do you understand that #send is tantamount to a keyword?

yes. but it’s only keword-like without a receive and

send! ‘private_method’

is fine since

private_method

is fine too.

-a

[email protected] wrote:

+1
yes. but it’s only keword-like without a receive and

send! ‘private_method’

is fine since

private_method

is fine too.

But it’s the public use that actually causes the most trouble. It’s
keyword-like in that it a method that is too important to override. So
we end up with send or __send, which is ridiculous. Might as well
drop #send altogether and just have the ugly shadow method. Please, ask
yourselves, why do we have send?

T.

On Thu, 4 Jan 2007, Trans wrote:

is fine too.

But it’s the public use that actually causes the most trouble. It’s
keyword-like in that it a method that is too important to override. So
we end up with send or __send, which is ridiculous. Might as well
drop #send altogether and just have the ugly shadow method. Please, ask
yourselves, why do we have send?

T.

hmmm. there is no way around this kind of thing unless the operation of
sending a message to an object is removed from the object’s interface.
so we
could have something like

Pervasives.send obj, message, *args, &block

here Pervasives is an object that cannot be changed - even by changing
Object#send.

this also allows

Pervasives.object_id obj

etc.

thoughts?

-a

On Thu, 4 Jan 2007 [email protected] wrote:

this also allows

Pervasives.object_id obj

etc.

thoughts?

I’m afraid I’m lost. What’s wrong with send, other than its
commonness as a name (which is dealt with by send)? If it’s too
much trouble to explain, that’s OK.

so long as we have to rely on objects responding to certain methods
certain
constructs, such as those that meta-programming cut to, can be made
difficult
since the very methods we require to operate can be over-ridden. this
is why
we have

id
send

are examples. by moving the essential methods outside of the object
itself an
into a frozen introspector

Introspector.object_id obj

we can elimnate this gotcha and this code

class BlankSlate
instance_methods.each{|m| remove_method unless m[/__/]}
end

can be reduced (and made more robust) to

class BlankSlate
instance_methods.each{|m| remove_method m}
end

make sense?

-a