Similar Threads:
1.Beginner 'How-to' Question on getting Array info
2.[proto-rcr] Blocks: default arguments and method signatures
I thought I'd post these ideas here, since last time I wrote up an RCR
and then got told that the issue had already been addressed in plans for
Ruby 2. Also, some ideas I'm pretty happy with, some are quite
radical/provocative. Hopefully those latter bits are somewhat
independent, which ones do you like? (if any ;)
So please let me know if I'm missing something, or this could be done
better.
1) Default arguments for &block
Often a method performs some simple reasonably useful behaviour if no
block is given, otherwise it lets the block do something more useful.
For example:
def transform_values(array)
out=[] unless block_given?
array.each { |value|
# calculations...
if block_given?
yield value,newvalue
else
out<< newvalue
end
}
out
end
I propose &block could take a default argument, probably of the form
&block={|x| foo}, but I could live with &block=proc {|x| foo}.
block_given? would return *false* if the default value was used (I'm
flexible on this bit).
The default block would be scoped *inside* the method.
That example would become:
def transform_values(array) &block={|val,newval| out<< newval}
out=[] unless block_given?
array.each { |value|
#calculations...
yield value,newvalue
}
end
2) Taking anonymous block parameter, making it part of the signature
Two issues:
a) If block default values are adopted, then giving the block a
name might sometimes seem silly, as the name's never used.
See the above example, "block" is never referenced.
b) No way to specify taking a block as part of method signature.
Descrptive signatures are good:
* See syntax in auto-docs/code without reading whole thing
* See syntax in auto-docs/code when there's no comments
* Flag errors at start of method, and on every invocation.
Proposal: allow "&" in place of "&block":
def foo & (or maybe: def foo &!)
As soon as the method is called, raises an error if no block is
given (similar to wrong number of args)
def foo &? (or maybe: def foo &)
No change from current behaviour of def foo, but denotes that
this method can take a block and might use it if given.
(MAYBE:)
def foo &block! or
def foo &!block or
replace current meaning(!) of: def foo &block
This method is required to take a block, not passing a block
raises an error.
def foo &block or
def foo &block? or
def foo &?block, etc
Current meaning of &block.
My preferred syntax in a throw-everything-away-for-Ruby2 scenario:
Named block Anonymous block
Disallowed def foo def foo
Optional def foo &block? def foo &?
Defaulted def foo &block={} def foo &={}
Compulsory def foo &block def foo &
My preferred syntax in a backwards-compatible scenario:
Named block Anonymous block
Disallowed - -
Optional def foo &block def foo &?
Defaulted def foo &block={} def foo &={}
Compulsory def foo &block! def foo &!
Thoughts?
Sam
3.Documenting a class interface when there are no types in the method signature
I've recently started using Ruby, coming from a C++/Java background.
And while there where some idiosyncracies, by and large I was able to be
very productive very quickly.
The application has evolved from a simple script (in the sense that
everything "looks global") to something a bit more complex, with
refactorings towards using objects, a "Test-First-After-The-Fact" approach
:-), and to factoring out library-like elements.
Now, I'm aware that there's always a discussion about strong vs. weak,
static vs. dynamic. The last time I had an opinion about it I had no good
answer to the claim that unit-tests lessen the need for static type
checking.
Now that I have gulped down the basics of ruby, I'm starting writing unit
tests, from the point of view of "what would a user of this class expect".
And I'm running into what seems to be a fundamental question, and my
googling so far hasn't turned up any satisfactory answer:
When I define a method in a class, let's say initialize(categories, data)
for the sake of argument: In Java etc. I can see from the method definition
that categories is an ordered set, and data is a Map (and if that info is
not sufficient, javadoc can be used to explain in more detail what is
expected, but I'll leave that out for the moment, it's not really key to the
question).
Of course the first step would be to find a good name (at the very least
"data" is a bit too generic). But what next? How does someone who writes an
API communicate that it makes no sense to send "1" to the categories.
Apparently the unit tests and/or dbc encompass the specification, but does
rdoc or some tool extract any information from them?
Am I the first person to run in this problem? If not, what are other
people's solutions? Do you just look at the source code of any library you
use?
I'm asking this from both points of view:
Many times I have run into some library where I was simply asking myself
"what exactly do I have to put into these parameters to get the method to do
what I want?" More specifically, I had this problem in parts of the REXML
library.
And from the other perspective, what is the Ruby Way (tm) to document
methods to users of your API/framework (or simply your class)?
4.getting info about attached displays
5.Problem with getting info from several websites
6. Getting test method names when getting output
7. FastRI 0.3.0: standalone mode (qri, DRb not needed), additional search methods, extended class info
8. Passing info to classes and methods