[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Random idea - private, blocks, constants

Daniel Berger

12/13/2006 8:23:00 PM

Hi all,

What do people think of the idea of private (and protected) taking a
block?

class Foo
private do
SOME_VALUE 88
def some_method
7
end
end
end

This wouldn't really be any different than the current behavior for
methods, but it would (in theory) allow you to make constants, class
variables and class instance variables private.

My main reason for wanting this behavior is that, in some cases, I
declare constant values that aren't meant for public use.

A good example is the windows-pr stuff, where I'm converting C macros
to Ruby values, or using constants to store function pointers from
Win32API. If you were to do 'require "win32/file"', and then print the
results of File.constants, you would see dozens of constants, e.g
File::INVALID_HANDLE_VALUE, File::DeviceIoControl, etc. Most of those
are not meant for public use and should not be visible directly by the
end user.

Come to think of it, perhaps we don't need the block. Just declare
that anything declared after 'private' is private, including constants,
etc.

Or is this concept just too radical?

Regards,

Dan

PS - I thought this had been brought up in the past, but I couldn't
find anything.

6 Answers

Joel VanderWerf

12/14/2006 3:26:00 AM

0

Daniel Berger wrote:
...
> My main reason for wanting this behavior is that, in some cases, I
> declare constant values that aren't meant for public use.
>
> A good example is the windows-pr stuff, where I'm converting C macros
> to Ruby values, or using constants to store function pointers from
> Win32API. If you were to do 'require "win32/file"', and then print the
> results of File.constants, you would see dozens of constants, e.g
> File::INVALID_HANDLE_VALUE, File::DeviceIoControl, etc. Most of those
> are not meant for public use and should not be visible directly by the
> end user.

What about putting them in a nested module?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Peter Hickman

12/14/2006 5:25:00 PM

0

Not wanting to troll you here but is this really a problem?

Adding complexity to the language to hide things but at the same time
requiring a way to circumvent the protection will achieve very little
for much work. Remember I could just read the source code and use them
with const_get.

If you provide good documentation for your module and the programmers
have no need to access these constants then they wont. I do all of my
programming using the docs, as I would guess most of the rest of us do.
We have better things to do with our time than to dig into your code, we
have stuff to write.

And then there is the ego thing. Do you honestly think that you know
better than every programmer who will ever use your module? There could
be a real need to get at those constants, you are not omnipotent so just
document your api and warn the users that if they use anything outside
of the documented api then on their head be it.

There is a theory about government that as time passes people will think
that they should pass some laws or something just to prove their own
vitality. There seems to come a point in with languages these days that
people feel that they need to add feature just to show the language is
alive.

I blame Microsoft :)

Again I am not trolling you but I don't see this adding something to
Ruby that is missing.


Daniel Berger

12/14/2006 7:11:00 PM

0


Peter Hickman wrote:
> Not wanting to troll you here but is this really a problem?
>
> Adding complexity to the language to hide things but at the same time
> requiring a way to circumvent the protection will achieve very little
> for much work. Remember I could just read the source code and use them
> with const_get.

That's fine. I'm not concerned with people getting at my constants if
they really want to. As things stand now the private/protected
keywords are are merely advisory anyway. But, they do have their uses.

> If you provide good documentation for your module and the programmers
> have no need to access these constants then they wont. I do all of my
> programming using the docs, as I would guess most of the rest of us do.
> We have better things to do with our time than to dig into your code, we
> have stuff to write.

The digging could end up happening on the outside, not the inside, and
that's what I'm trying to avoid. Say you're in an irb session and you
want to see what constants are available for the File class. Normally
this would just be a handful of values. If you were to do 'require
"win32/file"', however, you'll end up with a bunch of function pointer
declarations showing up in the output that you almost certainly don't
care about. It messes up class inspection.

> And then there is the ego thing. Do you honestly think that you know
> better than every programmer who will ever use your module? There could
> be a real need to get at those constants, you are not omnipotent so just
> document your api and warn the users that if they use anything outside
> of the documented api then on their head be it.

Do you object to the existence of private/protected then? You could
say the same thing for methods. As I said, private is advisory only,
but it does have its uses. For example, code coverage automation tools
could be configured to ignore private methods (which is what I'm
guessing most of them do by default).

> There is a theory about government that as time passes people will think
> that they should pass some laws or something just to prove their own
> vitality. There seems to come a point in with languages these days that
> people feel that they need to add feature just to show the language is
> alive.

I'm not sure how this makes Ruby more complicated. This adds very
little mental overhead and I think it would have very little impact on
existing code, since the majority of Ruby code that I've seen sticks
the public stuff at the top, and private/protected stuff at the bottom.

> Again I am not trolling you but I don't see this adding something to
> Ruby that is missing.

And that's what public discussion is for. It's just an idea. If Matz
and/or the majority of folks reading this message think it's stupid (or
too difficult to implement), then it will be rejected. Otherwise, it
will be accepted.

Regards,

Dan

Devin Mullins

12/15/2006 6:52:00 AM

0

Daniel Berger wrote:
> What do people think of the idea of private (and protected) taking a
> block?
>
> This wouldn't really be any different than the current behavior for
> methods, but it would (in theory) allow you to make constants, class
> variables and class instance variables private.
Hrm... I have no opinion on private constants, but I'm going to borrow
this thread to ask a related question. (Feel free to yell at me.)

What do people think of 'def' returning the name of the method? Then you
could do:
private def foo
blahblahblah
end
For those one-off private methods.

Devin
*turns on noise-cancelling headphones*

Peter Hickman

12/15/2006 10:02:00 AM

0

Daniel Berger wrote:
> The digging could end up happening on the outside, not the inside, and
> that's what I'm trying to avoid. Say you're in an irb session and you
> want to see what constants are available for the File class. Normally
> this would just be a handful of values. If you were to do 'require
> "win32/file"', however, you'll end up with a bunch of function pointer
> declarations showing up in the output that you almost certainly don't
> care about. It messes up class inspection.
>

Messy yes but is it a problem? Just open another shell and read the
documentation.

> Do you object to the existence of private/protected then? You could
> say the same thing for methods. As I said, private is advisory only,
> but it does have its uses. For example, code coverage automation tools
> could be configured to ignore private methods (which is what I'm
> guessing most of them do by default).
>

Actually I would be quite happy with just public and private, I'm not
sure that I have ever used protected but I'm willing to accept that
other people find it useful.

I earn my crust as a Perl programmer and everything is public if you
used the normal blessed hash technique for objects, Perl objects are
hack anyway, and guess what - it is not a problem! I realise that for
anyone from a static background (C/C++/Java/C#) will feel nervous
without their belt, braces, safety net and lucky rabbit's foot but to be
honest the problems that all this keyword verbiage protects you against
is more scaremongering than real. No language, no matter how B&D, will
stop you from writing bad or broken code. No one will become better a
programmer because a new keyword is added to the language.


Daniel Berger

12/15/2006 8:03:00 PM

0

Devin Mullins wrote:
> Daniel Berger wrote:
>> What do people think of the idea of private (and protected) taking a
>> block?
>>
>> This wouldn't really be any different than the current behavior for
>> methods, but it would (in theory) allow you to make constants, class
>> variables and class instance variables private.
> Hrm... I have no opinion on private constants, but I'm going to borrow
> this thread to ask a related question. (Feel free to yell at me.)
>
> What do people think of 'def' returning the name of the method? Then you
> could do:
> private def foo
> blahblahblah
> end
> For those one-off private methods.
>
> Devin
> *turns on noise-cancelling headphones*
>
>

This has been proposed before (i.e. have 'def' return a symbol). I
don't know if Matz implemented this for 1.9 or not. It won't make it
into 1.8, though.

http://oldrcrs.rubypal.com/rc...

Regards,

Dan