Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Random idea - private, blocks, constants

Reply
Thread Tools

Random idea - private, blocks, constants

 
 
Daniel Berger
Guest
Posts: n/a
 
      12-13-2006
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:eviceIoControl, 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.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      12-14-2006
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:eviceIoControl, 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

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      12-14-2006

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Random idea - private, blocks, constants"
> on Thu, 14 Dec 2006 05:25:11 +0900, "Daniel Berger" <(E-Mail Removed)> writes:
>
> |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.
>
> Hmm, first we have to define how private constants (and class
> variables) behave.
>
> Class variables are not supposed to be accessed outside of a class
> body. So they are always private, I guess. We don't need special
> treatment for them.


True.

> But how should private constants behave? Maybe
> making them not accessible via :: operator is suffice. I'm not sure
> yet.


That seems reasonable, so long as you could still cheat, and get at
them via const_get if you wanted to (for testing purposes, for
example). To me that would be roughly analogous to private methods
that are still accessible via send.

> The next thing I need to think about is how we can implement them.
> But it's another story. As far as I can imagine now, it surely is
> difficult, since currently there's no place to save visibility
> information in the constants table.


I poked around in eval.c to see if I could come up with a patch on my
own, but I think it's beyond me.

Regards,

Dan

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      12-14-2006
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.


 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-14-2006

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

 
Reply With Quote
 
Sam Smoot
Guest
Posts: n/a
 
      12-15-2006

Yukihiro Matsumoto wrote:
> prints 1 and "bar", so that they need not to be protected by
> visibility. They are, even if they are accepted, just for narrowing
> constants list.
>
> matz.


In light of that, might the solution here simply be sub-classing the
objects involved (such as File) instead of decorating the existing
classes?

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-15-2006
Sam Smoot wrote:
> Yukihiro Matsumoto wrote:
>> prints 1 and "bar", so that they need not to be protected by
>> visibility. They are, even if they are accepted, just for narrowing
>> constants list.
>>
>> matz.

>
> In light of that, might the solution here simply be sub-classing the
> objects involved (such as File) instead of decorating the existing
> classes?


Most of the time I would, but there are times when you want to redefine
methods of existing classes.

Also, C++ and C# seem to have the concept of private constants. What
they're used for in practice, though, I'm not sure. I'll have to
research more.

Regards,

Dan


 
Reply With Quote
 
Devin Mullins
Guest
Posts: n/a
 
      12-15-2006
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*

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      12-15-2006
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.


 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-15-2006
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/rcr/show/277

Regards,

Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM



Advertisments