Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Seeking advice on some method names

Reply
Thread Tools

Seeking advice on some method names

 
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-29-2004
Hi all,

I'm preparing some methods for 'extensions' [1] and would like some
people's opinions on method names.

Array#only
[5].only -> 5
[1,2,3].only -> exception

Class.get_class
Class.get_class("Test::Unit") -> Test::Unit
Class.get_class("not-a-class") -> exception

# Should 'get_class' be in Class or in Kernel?

Class#bare_name
Test::Unit.bare_name -> "Unit"

String#line_wrap
"the quick brown fox jumps ...".line_wrap(10)
-> "the quick
brown fox
jumps ..."

Thanks,
Gavin

[1] http://extensions.rubyforge.org



 
Reply With Quote
 
 
 
 
James Britt
Guest
Posts: n/a
 
      11-29-2004
Gavin Sinclair wrote:
> Hi all,
>
> I'm preparing some methods for 'extensions' [1] and would like some
> people's opinions on method names.
>
> Array#only
> [5].only -> 5
> [1,2,3].only -> exception
>
>


What is 'only' supposed to do?


James


 
Reply With Quote
 
 
 
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-29-2004
On Monday, November 29, 2004, 2:06:25 PM, James wrote:

> Gavin Sinclair wrote:
>> Hi all,
>>
>> I'm preparing some methods for 'extensions' [1] and would like some
>> people's opinions on method names.
>>
>> Array#only
>> [5].only -> 5
>> [1,2,3].only -> exception
>>
>>


> What is 'only' supposed to do?


Return the _only_ element in an array. If there's not exactly one,
then "only" doesn't make sense, so an exception is raised. (At the
moment, the exception is RangeError, but I need to think about that.)

Gavin




 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-29-2004
On Monday, November 29, 2004, 1:57:50 PM, itsme213 wrote:

>> Array#only
>> [5].only -> 5
>> [1,2,3].only -> exception


> I prefer: Array#one
> to be complemented by
> Array#one? #{|e| predicate(e)} true if exactly one match


> and rounded out by

Array#any #=>> return a random element, like Array#any?
> Array#none # with addition of Array#none? {|e| predicate(e)}

Array#all #=>> of questionable value

> and separately
> Array#include_all? (other_enumerable)


#any? and #all? are methods of Enumerable, not Array. I like the idea
of Enumerable#one? and Enumerable#none? to complement them.

But I don't think Enumerable#one? and Array#one are complementary;
they seem entirely different.

Likewise with Array#any: it's useful, but really has nothing to do
with #any? I'm implementing Array#rand, by the way.

Regarding Array#include_all?, you might want to look at Set.

Cheers,
Gavin



 
Reply With Quote
 
Sam Stephenson
Guest
Posts: n/a
 
      11-29-2004
On Mon, 29 Nov 2004 11:31:06 +0900, Gavin Sinclair
<(E-Mail Removed)> wrote:
> Class.get_class
> Class.get_class("Test::Unit") -> Test::Unit
> Class.get_class("not-a-class") -> exception
>
> # Should 'get_class' be in Class or in Kernel?


Why not String?
"Test::Unit".to_class # => Test::Unit

My justification: it's analogous to String#to_sym.

> Thanks,
> Gavin


Sam


 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-29-2004
Hi --

On Mon, 29 Nov 2004, Sam Stephenson wrote:

> On Mon, 29 Nov 2004 11:31:06 +0900, Gavin Sinclair
> <(E-Mail Removed)> wrote:
> > Class.get_class
> > Class.get_class("Test::Unit") -> Test::Unit
> > Class.get_class("not-a-class") -> exception
> >
> > # Should 'get_class' be in Class or in Kernel?

>
> Why not String?
> "Test::Unit".to_class # => Test::Unit
>
> My justification: it's analogous to String#to_sym.


I'm not sure what either of these adds to const_get -- or, to put it
another way, why there should be a special method to do (essentially)
a const_get only for strings that represent class names.


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Jamis Buck
Guest
Posts: n/a
 
      11-29-2004
David A. Black wrote:
> Hi --
>
> On Mon, 29 Nov 2004, Sam Stephenson wrote:
>
>
>>On Mon, 29 Nov 2004 11:31:06 +0900, Gavin Sinclair
>><(E-Mail Removed)> wrote:
>>
>>> Class.get_class
>>> Class.get_class("Test::Unit") -> Test::Unit
>>> Class.get_class("not-a-class") -> exception
>>>
>>> # Should 'get_class' be in Class or in Kernel?

>>
>>Why not String?
>> "Test::Unit".to_class # => Test::Unit
>>
>>My justification: it's analogous to String#to_sym.

>
>
> I'm not sure what either of these adds to const_get -- or, to put it
> another way, why there should be a special method to do (essentially)
> a const_get only for strings that represent class names.


Well, IIRC, const_get doesn't work with symbols that contain "::". In
other words, const_get( "Test::Unit" ) would fail--you'd have to do
const_get("Test").const_get("Unit"), which becomes cumbersome. I can
think of several instances in my Net::SSH and Net::SFTP stuff alone
where a standard #get_class method would have been useful (though far
from necessary).

- Jamis

--
Jamis Buck
(E-Mail Removed)
http://www.jamisbuck.org/jamis


 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-29-2004
Hi --

On Mon, 29 Nov 2004, Jamis Buck wrote:

> David A. Black wrote:
> > Hi --
> >
> > On Mon, 29 Nov 2004, Sam Stephenson wrote:
> >
> >
> >>On Mon, 29 Nov 2004 11:31:06 +0900, Gavin Sinclair
> >><(E-Mail Removed)> wrote:
> >>
> >>> Class.get_class
> >>> Class.get_class("Test::Unit") -> Test::Unit
> >>> Class.get_class("not-a-class") -> exception
> >>>
> >>> # Should 'get_class' be in Class or in Kernel?
> >>
> >>Why not String?
> >> "Test::Unit".to_class # => Test::Unit
> >>
> >>My justification: it's analogous to String#to_sym.

> >
> >
> > I'm not sure what either of these adds to const_get -- or, to put it
> > another way, why there should be a special method to do (essentially)
> > a const_get only for strings that represent class names.

>
> Well, IIRC, const_get doesn't work with symbols that contain "::". In
> other words, const_get( "Test::Unit" ) would fail--you'd have to do
> const_get("Test").const_get("Unit"), which becomes cumbersome. I can
> think of several instances in my Net::SSH and Net::SFTP stuff alone
> where a standard #get_class method would have been useful (though far
> from necessary).


Oh right, I forgot about that. There are some "deep" const_get
implementations floating around... along the lines of:

def deep_const_get(str)
str.split('::').inject(Object) {|a,b| a.const_get(b) }
end

which might help.


David

--
David A. Black
(E-Mail Removed)



 
Reply With Quote
 
Sam Stephenson
Guest
Posts: n/a
 
      11-29-2004
On Mon, 29 Nov 2004 13:34:24 +0900, David A. Black <(E-Mail Removed)> wrote:
> Hi --
>
>
>
> On Mon, 29 Nov 2004, Sam Stephenson wrote:
>
> > On Mon, 29 Nov 2004 11:31:06 +0900, Gavin Sinclair
> > <(E-Mail Removed)> wrote:
> > > Class.get_class
> > > Class.get_class("Test::Unit") -> Test::Unit
> > > Class.get_class("not-a-class") -> exception
> > >
> > > # Should 'get_class' be in Class or in Kernel?

> >
> > Why not String?
> > "Test::Unit".to_class # => Test::Unit
> >
> > My justification: it's analogous to String#to_sym.

>
> I'm not sure what either of these adds to const_get


AFAIK, const_get doesn't handle objects below its namespace, so you
can't const_get 'Foo::Bar', you have to Foo.const_get 'Bar'.

> -- or, to put it
> another way, why there should be a special method to do (essentially)
> a const_get only for strings that represent class names.


I agree. Maybe String#to_const would be more appropriate?

> David
>
> --
> David A. Black
> (E-Mail Removed)


Sam


 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      11-29-2004
> On Mon, 29 Nov 2004, Sam Stephenson wrote:
> > Why not String?
> > "Test::Unit".to_class # => Test::Unit


On Sunday 28 November 2004 11:34 pm, David A. Black wrote:
> I'm not sure what either of these adds to const_get -- or, to put it
> another way, why there should be a special method to do (essentially)
> a const_get only for strings that represent class names.


Why? Because it is a common function that is easy to do wrong. The quick and
obvious choices (eval(string) and Object.const_get(string)) are flawed. And
the one-liner that does it correctly ...

string.split("::").inject(Object) { |ns, n| ns.const_get(n) }

... is hardly transparent.

It is a common enough activity that having a standard and canonical way of
converting strings to classes would ease a number of tasks (e.g. anytime you
wish to specify a class in a config file, input stream or data base).

--
-- Jim Weirich (E-Mail Removed) http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


 
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
method names-how to indicate get vs set type of method? Greg Hauptmann Ruby 1 11-06-2008 03:19 AM
Seeking some advice and/or pointers as to getting into web-based java application dev using Struts NickName Java 4 12-17-2006 07:44 PM
Give me some advice about some books of Java programing! yuyazhang Java 14 04-29-2006 11:13 AM
Re: fetching method names from a class,and the parameter list from a method John Lenton Python 0 01-11-2005 05:12 AM
Seeking advice on what to buy for Win98/XP home network... Sunny Side Wireless Networking 7 07-31-2004 01:47 AM



Advertisments