Little Things

Hi --

···

On Sun, 31 Dec 2006, Rob Sanheim wrote:

On 12/31/06, Devin Mullins <twifkak@comcast.net> wrote:

Trans wrote:
> Hmm... Are you perhpas indirectly suggesting:
>
> send(:meth) # private
> self.send(:meth) # public
>
> ?

Yikes, a method that behaves differently depending on how it's called?
What about method(:send).call vs self.method(:send).call?

Meprefers send_priv or something.

Devin

What about:

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

reads better then funcall to me, at least.

Me too -- that was what I suggested long ago :slight_smile: Matz didn't feel it
corresponded to his concept of a ! (dangerous) method.

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

What about:

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

I don't normally weigh in on these discussions, but since it's come up again I'm strongly +1 for this.

reads better then funcall to me, at least.

I know very little lisp, and though the private method == function link makes sense, it's pretty esoteric.

I don't know what I'd expect a funcall method to do ... dial 0800 FUN NOW ?

happy new year
alex

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 1/1/07, Trans <transfire@gmail.com> wrote:

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

} Dave Thomas 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

···

On Tue, Jan 02, 2007 at 12:04:35AM +0900, Trans wrote:

+1

···

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

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

+1. This sounds great. (sorry for the late reply)

···

On 12/31/06, Rob Sanheim <rsanheim@gmail.com> wrote:

On 12/31/06, Devin Mullins <twifkak@comcast.net> wrote:

What about:

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

No please. That doesn't match the behavior of any other #foo, #foo! that I know of. #send and #funcall match what the written methods look like perfectly.

···

On Dec 31, 2006, at 24:04, Wilson Bilkovich wrote:

On 12/31/06, Rob Sanheim <rsanheim@gmail.com> wrote:

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

reads better then funcall to me, at least.

Yes please. That's perfect.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Hi,

···

In message "Re: Little Things" on Sun, 31 Dec 2006 21:12:07 +0900, dblack@wobblini.net writes:

Me too -- that was what I suggested long ago :slight_smile: Matz didn't feel it
corresponded to his concept of a ! (dangerous) method.

I often change my mind, especially for 1.9. See Mauricio's post in
[ruby-talk:231912].

              matz.

Gregory Seidman 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.

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'd put #2 before #1 and often 'def obj.meth' serves the purpose.

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

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.

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

Gary Wright

···

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

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

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 Sanheim 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

Hi --

···

On Thu, 4 Jan 2007, Gregory Brown wrote:

On 12/31/06, Rob Sanheim <rsanheim@gmail.com> wrote:

On 12/31/06, Devin Mullins <twifkak@comcast.net> wrote:

What about:

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

+1. This sounds great. (sorry for the late reply)

BTW the earlier discussion of this, for those interested, starts at:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/153833

So everyone be careful not to add your +1 if it's already there :slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

The problem with #send and #funcall (or #send!) is that these are VIMs
(very important methods). They aren't to be overwritten lightly, if at
all, which is evident by the fact that we also have __send__ in case
that it is. So any generic piece of meta-programming ends up using
__send__ anyway. Throw in __funcall__ and now we have four methods when
two would do. So besides the fact that this straddles us with two
completely different terms for nearly the exact same thing, which is
bad enough, it does nothing to alleviate this more fundamental issue.

T.

···

On Dec 31, 3:34 am, Eric Hodel <drbr...@segment7.net> wrote:

On Dec 31, 2006, at 24:04, Wilson Bilkovich wrote:

> On 12/31/06, Rob Sanheim <rsanh...@gmail.com> wrote:
>> obj.send(:foo) # will call only public
>> obj.send!(:foo) # will bypass private/protected, like the current
>> send.

>> reads better then funcall to me, at least.

> Yes please. That's perfect.No please. That doesn't match the behavior of any other #foo, #foo!
that I know of. #send and #funcall match what the written methods
look like perfectly.

Hi --

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

reads better then funcall to me, at least.

Yes please. That's perfect.

No please. That doesn't match the behavior of any other #foo, #foo! that I know of. #send and #funcall match what the written methods look like perfectly.

But #foo/#foo! behaviors don't have to match each other, except at the
very abstract level of one being "dangerous". I've always thought
that including private methods vs. not including them was a good
candidate for dangerous, but Matz didn't agree in this case so I don't
think it will happen.

#send and #funcall sort of match what the written methods *might* look
like (sometimes; not in the case of setter methods, and not for public
methods where you used a receiver) if you called them. It just seems
to me to be a pretty tenuous connection.

David

···

On Sun, 31 Dec 2006, Eric Hodel wrote:

On Dec 31, 2006, at 24:04, Wilson Bilkovich wrote:

On 12/31/06, Rob Sanheim <rsanheim@gmail.com> wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Just one quick comment:

"[...] what could potentially kill the language.
We can have the best language on the Planet, but if the language
features are only accessible to those that are willing to go code-diving
for the answers, then the target audience is going to be slim."

I disagree to the potential of this.
I myself am sometimes annoyed at the lack of any
documentation, this sometimes involve old code - new code
almost always is nicely documented :slight_smile:

However, only a lack of docu wont stop me from using
ruby. Most naturally because I do not HAVE to use 100%
of the language, and books like the pickaxe really
did help a LOT - IF any docu has to be added, then
i'd look at these things first :wink:

- More focus on the C part of Ruby. Its a little bit
difficult to find enough good quality documents about
extending Ruby in C
- Allow only code into stdlib etc.. that has a minimum
docu
- Replacement for ri/rdoc or updating, so many people
rely on it for documenting their code, but the
html frame alone isnt always that appealing, and
ri can sometimes be slow, silly or... well
- Ruby website should offer online docu similar to how
php does. Php as a language is a curse, but the
online docu is rather good. I think ruby should
take up the idea of providing good online docu.

Anyway... just some of my long 2 cc, dont mean
to interrupt! :smiley:

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Mon, 1 Jan 2007, Yukihiro Matsumoto wrote:

Hi,

In message "Re: Little Things" > on Sun, 31 Dec 2006 21:12:07 +0900, dblack@wobblini.net writes:

>Me too -- that was what I suggested long ago :slight_smile: Matz didn't feel it
>corresponded to his concept of a ! (dangerous) method.

I often change my mind, especially for 1.9. See Mauricio's post in
[ruby-talk:231912].

I saw -- it was a good reminder that I need to catch up on the latest
1.9's :slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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.

···

On 1/3/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

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

> On 2006/12/31, at 07:38, Rob Sanheim wrote:
>> obj.send(:foo) # will call only public
>> obj.send!(:foo) # will bypass private/protected, like the current send.
>
> +1

++

-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

--
"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

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.

···

ara.t.howard@noaa.gov wrote:

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

> On 2006/12/31, at 07:38, Rob Sanheim wrote:
>> obj.send(:foo) # will call only public
>> obj.send!(:foo) # will bypass private/protected, like the current send.
>
> +1

++

So everyone be careful not to add your +1 if it's already there :slight_smile:

What is the meaning of "+1" and "+" ? This list seems to use it
extensively

David, thanks for linking this. It's good to see this has come up
before and have some additional context.

···

On 1/3/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Thu, 4 Jan 2007, Gregory Brown wrote:

> On 12/31/06, Rob Sanheim <rsanheim@gmail.com> wrote:
>> On 12/31/06, Devin Mullins <twifkak@comcast.net> wrote:
>
>> What about:
>>
>> obj.send(:foo) # will call only public
>> obj.send!(:foo) # will bypass private/protected, like the current send.
>
> +1. This sounds great. (sorry for the late reply)

BTW the earlier discussion of this, for those interested, starts at:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/153833