This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
---
|
||||
revision: 2013
|
||||
type: ruby
|
||||
sources:
|
||||
- Indexfile
|
||||
- Gemfile
|
||||
authors:
|
||||
- name: Thomas Sawyer
|
||||
email: transfire@gmail.com
|
||||
organizations:
|
||||
- name: Rubyworks
|
||||
requirements:
|
||||
- groups:
|
||||
- test
|
||||
version: ">= 0"
|
||||
name: lemon
|
||||
- groups:
|
||||
- test
|
||||
version: ">= 0"
|
||||
name: qed
|
||||
- groups:
|
||||
- test
|
||||
version: ">= 0"
|
||||
name: rubytest-cli
|
||||
- groups:
|
||||
- test
|
||||
version: ">= 0"
|
||||
name: rake
|
||||
- groups:
|
||||
- test
|
||||
version: ">= 0"
|
||||
name: simplecov
|
||||
- groups:
|
||||
- guard
|
||||
version: ">= 0"
|
||||
name: guard
|
||||
conflicts: []
|
||||
alternatives: []
|
||||
resources:
|
||||
- type: home
|
||||
uri: http://rubyworks.github.com/facets
|
||||
label: Homepage
|
||||
- type: code
|
||||
uri: http://github.com/rubyworks/facets
|
||||
label: Source Code
|
||||
- type: docs
|
||||
uri: http://rubyworks.github.com/facets/learn.html
|
||||
label: Documentation
|
||||
- type: wiki
|
||||
uri: http://wiki.github.com/rubyworks/facets
|
||||
label: User Guide
|
||||
- type: talk
|
||||
uri: http://groups.google.com/group/facets-universal
|
||||
repositories:
|
||||
- name: upstream
|
||||
scm: git
|
||||
uri: git@github.com:rubyworks/facets.git
|
||||
categories: []
|
||||
copyrights:
|
||||
- holder: Rubyworks
|
||||
year: '2004'
|
||||
license: BSD-2-Clause
|
||||
customs: []
|
||||
paths:
|
||||
load:
|
||||
- lib/core
|
||||
- lib/standard
|
||||
name: facets
|
||||
title: Ruby Facets
|
||||
version: 3.1.0
|
||||
summary: The orginal well curated collection of extension methods for Ruby.
|
||||
slogan: ALL YOUR BASE ARE BELONG TO RUBY!
|
||||
description: Facets is the premier collection of extension methods for the Ruby programming
|
||||
language. Facets extensions are unique by virtue of thier atomicity. They are stored
|
||||
in individual files allowing for highly granular control of requirements. In addition,
|
||||
Facets includes a few additional classes and mixins suitable to wide variety of
|
||||
applications.
|
||||
scm_uri: https://github.com/rubyworks/facets/tree/master
|
||||
created: '2004-12-16'
|
||||
date: '2016-05-09'
|
||||
@@ -0,0 +1,10 @@
|
||||
--title "Ruby Facets"
|
||||
--readme README.rdoc
|
||||
--protected
|
||||
--private
|
||||
--tag uncommon:Uncommon
|
||||
--tag standard:Standard
|
||||
lib/core
|
||||
lib/standard
|
||||
-
|
||||
[A-Z]*.*
|
||||
@@ -0,0 +1,112 @@
|
||||
# CONTRIBUTING TO FACETS
|
||||
|
||||
## General Rules
|
||||
|
||||
* Facets methods must have no external dependencies. (The only possible
|
||||
exception to this is if the functor gets spun-off as a separate gem along
|
||||
with related methods, but for now that hasn't happened.)
|
||||
|
||||
* Each method must be in it's own file of the same name. If the method ends
|
||||
with `=` or `?` just leave it off the file name.
|
||||
|
||||
* On rare occasion two or more methods can be very tightly related. In these
|
||||
cases the methods can all go in a single file under the name of the method
|
||||
considered most significant. Usually files for the other methods should
|
||||
still be created that simply require the main file.
|
||||
|
||||
* Methods must be tested both via a Lemon unit test and as a QED demo.
|
||||
The Lemon unit tests are for testing a method in detail whereas the QED
|
||||
demos are for demonstrating usage.
|
||||
|
||||
* Facets is divided into two parts, *core* and *standard* libraries.
|
||||
Almost all of the core library can be loaded at once using `require 'facets'`
|
||||
The standard library (also called the *more* library) must be required
|
||||
per-script.
|
||||
|
||||
* Some core methods are included on a *trial* basis, and these are not
|
||||
necessary loaded automatically with `require 'facets'`. These should be
|
||||
documented as such in the method comments.
|
||||
|
||||
* Standard libraries that are not extensions of existing standard libraries
|
||||
do not have to be divvied up into individual method files. But note that
|
||||
full classes and/or modules are less likely to make it into Facets, as
|
||||
these sorts of additions to Facets are only for very basic sorts of things.
|
||||
Otherwise they'd deserve their own gem.
|
||||
|
||||
* When submitting new methods for consideration, it is best if each method
|
||||
(or *tightly related* set of methods) is in it's own pull request. If you
|
||||
have only one method to submit then a simple commit will do the trick. If
|
||||
you have more than one it best to use separate branches. Let me emphasizes
|
||||
this point because it makes it *much more likely* that your pull request
|
||||
will be merged. If you submit a bunch of methods in a single pull request,
|
||||
it is very likely that it will not be merged even if methods you submitted
|
||||
are accepted!!!
|
||||
|
||||
* Don't be discouraged when you get feedback about a method that isn't
|
||||
all sunshine and roses. Facets has been around long enough now that
|
||||
it needs to maintain a certain degree of quality control, and that means
|
||||
serious discernment about what goes into the library. That includes
|
||||
having in depth discussions the merits of methods, even about the best
|
||||
name for a method --even if the functionality has been accepted the name
|
||||
may not.
|
||||
|
||||
|
||||
## Versioning
|
||||
|
||||
Facets tries to follow a semantic versioning system, but with a slightly
|
||||
differt scheme than most projects. For Facets the version number represents:
|
||||
|
||||
gestault.major.minor
|
||||
|
||||
Techinically there can be a fouth `build` number, but we never use it for
|
||||
releases.
|
||||
|
||||
|
||||
## Commit Tags
|
||||
|
||||
When making a commit, it is helpful to add a *commit tag* to the end of the
|
||||
first line of commit message. Commit tags are single words wrapped in colons.
|
||||
|
||||
* If the commit is only a documentation change, then and `:doc:`.
|
||||
* If it is only a change to tests then add `:test:`.
|
||||
* If the change only effects project build or config files then add `:admin:`.
|
||||
* If the change fixes a bug that was reported via the issue system be sure
|
||||
to reference the issue number in the message using `#` and add a `:bug:` tag.
|
||||
* For actual code changes, if the change is *very minor* and not something
|
||||
anyone would notice, you can use `:tweak:`, if you want.
|
||||
* If the change would require a minor version change than use `:minor:`.
|
||||
* But if a change is a significant change to the API, and thus will require
|
||||
a major version change, then end the message with `:major:`.
|
||||
|
||||
These are all rules of thumb, and no one expects them to be applied perfectly.
|
||||
|
||||
|
||||
## Documentation
|
||||
|
||||
Facets started when the only choice for API documentation was RDoc. So originally
|
||||
that's how methods were documented. Since then both YARD and Tomdoc have come
|
||||
along. And some of these documentation styles have crept into a number of
|
||||
methods. So right now, things are a bit messy. But going forward it looks like we
|
||||
are going to settle on Tomdoc as the official documentation style (but using
|
||||
the tomparse gem's extensions). Using Tomdoc will give us reasonable
|
||||
interoperability with both RDoc and YARD, both of which now have support for
|
||||
Tomdoc (albeit support is not 100% the same, but hopefully it's close enough).
|
||||
|
||||
Officially we publish documentation via rubydoc.info, which is the YARD server,
|
||||
and via the Facets website in Shomen JSON format.
|
||||
|
||||
When writing documentation for a method it is best to give a simple summary
|
||||
explanation, followed by some basic examples. Follow that up with deeper
|
||||
explanation if needed, including *when* and *why* the method could be useful.
|
||||
|
||||
|
||||
## Testing
|
||||
|
||||
* Methods in `lib/core/facets/{class}/{method}.rb` will be tested in `test/foo/{class}/test_{method}.rb`.
|
||||
* If `lib/core/facets/{class}/{method}.rb` consists only of a require statement, no test file is expected.
|
||||
* If `lib/core/facets/{class}/{method}.rb` consists only of a require and an alias, then `test/foo/{class}/{method}.rb`, only needs to test the existence of the alias and not the underlying code. But it's okay if the alias is tested further.
|
||||
* Methods in `lib/core/facets/{class}/{method}.rb` will be demoed in `demo/core/{class}/{method}.md`.
|
||||
* Require only files will have a full demo of it's method or methods. Code in a single file may be split into multiple demos, named after the method. This is to promote discoverability in the documentation.
|
||||
* Demos of aliases will have a simple demo, and a reference to the file it aliases
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,349 @@
|
||||
|
||||
Ruby Facets
|
||||
|
||||
Copyright (c) 2005,2010 Thomas Sawyer
|
||||
|
||||
|
||||
THE RUBY LICENSE
|
||||
(http://www.ruby-lang.org/en/LICENSE.txt)
|
||||
|
||||
You may redistribute this software and/or modify it under either the terms of
|
||||
the GPL (see below), or the conditions below:
|
||||
|
||||
1. You may make and give away verbatim copies of the source form of the
|
||||
software without restriction, provided that you duplicate all of the
|
||||
original copyright notices and associated disclaimers.
|
||||
|
||||
2. You may modify your copy of the software in any way, provided that
|
||||
you do at least ONE of the following:
|
||||
|
||||
a) place your modifications in the Public Domain or otherwise
|
||||
make them Freely Available, such as by posting said
|
||||
modifications to Usenet or an equivalent medium, or by allowing
|
||||
the author to include your modifications in the software.
|
||||
|
||||
b) use the modified software only within your corporation or
|
||||
organization.
|
||||
|
||||
c) rename any non-standard executables so the names do not conflict
|
||||
with standard executables, which must also be provided.
|
||||
|
||||
d) make other distribution arrangements with the author.
|
||||
|
||||
3. You may distribute the software in object code or executable
|
||||
form, provided that you do at least ONE of the following:
|
||||
|
||||
a) distribute the executables and library files of the software,
|
||||
together with instructions (in the manual page or equivalent)
|
||||
on where to get the original distribution.
|
||||
|
||||
b) accompany the distribution with the machine-readable source of
|
||||
the software.
|
||||
|
||||
c) give non-standard executables non-standard names, with
|
||||
instructions on where to get the original software distribution.
|
||||
|
||||
d) make other distribution arrangements with the author.
|
||||
|
||||
4. You may modify and include the part of the software into any other
|
||||
software (possibly commercial). But some files in the distribution
|
||||
are not written by the author, so that they are not under these terms.
|
||||
|
||||
For the list of those files and their copying conditions, see the
|
||||
file LEGAL.
|
||||
|
||||
5. The scripts and library files supplied as input to or produced as
|
||||
output from the software do not automatically fall under the
|
||||
copyright of the software, but belong to whomever generated them,
|
||||
and may be sold commercially, and may be aggregated with this
|
||||
software.
|
||||
|
||||
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE.
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Library General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
= COPYRIGHT NOTICES
|
||||
|
||||
== Ruby Facets
|
||||
|
||||
Copyright:: (c) 2005,2011 Thomas Sawyer
|
||||
|
||||
Copyright (c) 2005,2011 Thomas Sawyer
|
||||
|
||||
You may distribute this software under the same terms as Ruby.
|
||||
|
||||
|
||||
== Ruby/Extensions
|
||||
|
||||
Copyright:: (c) 2004 Gavin Sinclair
|
||||
License:: Ruby
|
||||
|
||||
Copyright (c) 2004 Gavin Sinclair
|
||||
|
||||
Ruby/Extensions (extensions.rubyforge.org) is copyrighted free software
|
||||
created and maintained by Gavin Sinclair (gsinclair@soyabean.com.au) and
|
||||
released under the same license as Ruby.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE.
|
||||
|
||||
|
||||
== Nitro/Glue
|
||||
|
||||
Copyright:: (c) 2006, George Moschovitis
|
||||
License:: BSD-3-Clause
|
||||
|
||||
Copyright (c) 2006, George Moschovitis
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the <ORGANIZATION> nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
== ActiveSupport
|
||||
|
||||
Copyright:: (c) 2005-2011 David Heinemeier Hansson
|
||||
License:: MIT
|
||||
Website:: http://rails.org
|
||||
|
||||
Copyright (c) 2005-2011 David Heinemeier Hansson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
== Ruby Treasures
|
||||
|
||||
Copyright:: (c) 2001 Paul Brannan
|
||||
License:: Ruby License
|
||||
|
||||
Ruby Treasures 0.1
|
||||
Copyright (c) 2001 Paul Brannan <paul@atdesk.com>
|
||||
|
||||
You may distribute this software under the same terms as Ruby (see the file
|
||||
RUBY.txt that was distributed with this library).
|
||||
|
||||
|
||||
== Metaid
|
||||
|
||||
Copyright:: (c) why the lucky stiff
|
||||
|
||||
Copyright (c) why the lucky stiff
|
||||
|
||||
|
||||
== CLAP
|
||||
|
||||
Copyright:: (c) 2010 Michel Martens
|
||||
License:: MIT
|
||||
Website:: https://github.com/soveran/clap
|
||||
|
||||
Copyright (c) 2010 Michel Martens
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to
|
||||
do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
|
||||
|
||||
== String#natcmp
|
||||
|
||||
Copyright:: (c) 2003 by Alan Davies
|
||||
License:: Custom
|
||||
|
||||
String#natcmp is based on Martin Pool's "Natural Order String Comparison"
|
||||
originally written in C. (see http://sourcefrog.net/projects/natsort/)
|
||||
|
||||
This implementation is Copyright (C) 2003 by Alan Davies
|
||||
<cs96and_AT_yahoo_DOT_co_DOT_uk>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
# Ruby Facets
|
||||
|
||||
[](http://badge.fury.io/rb/facets)
|
||||
[](http://travis-ci.org/rubyworks/facets)
|
||||
[](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund)
|
||||
|
||||
|
||||
*"ALL YOUR BASE ARE BELONG TO RUBY"*
|
||||
|
||||
|
||||
## Introduction
|
||||
|
||||
Ruby Facets is the premiere collection of general purpose method
|
||||
extensions and standard additions for the Ruby programming language.
|
||||
|
||||
Facets houses the largest single collection of methods available for
|
||||
extending the core capabilities of Ruby's built-in classes and modules.
|
||||
This collection of extension methods are unique by virtue of their atomicity.
|
||||
The methods are stored in individual files so that each can be required
|
||||
independently. This gives developers the potential for much finer control over
|
||||
which extra methods to bring into their code.
|
||||
|
||||
In addition Facets provides a collection of extensions to Ruby standard library
|
||||
plus a small collection of add-on classes and modules. Together these
|
||||
libraries constitute an reliable source of reusable components, suitable
|
||||
to a wide variety of usecases.
|
||||
|
||||
|
||||
## Resources
|
||||
|
||||
* Homepage: http://rubyworks.github.com/facets
|
||||
* Report Bugs: http://github.com/rubyworks/facets/issues
|
||||
* Mailing List: http://groups.google.com/group/facets-universal/topics
|
||||
* Wiki Pages: http://wiki.github.com/rubyworks/facets
|
||||
* Source Code: http://github.com/rubyworks/facets
|
||||
|
||||
|
||||
## Documentation
|
||||
|
||||
Facets has special documentation needs due to its extensive breadth.
|
||||
The documentation generated when installing via RubyGems, or the YARD
|
||||
docs provided by rubydoc.info can be somewhat unwieldy because it
|
||||
combines all of Facets in one large set. When using these resources,
|
||||
it is important to remain aware of the source location of particular
|
||||
methods.
|
||||
|
||||
For better organized online documentation, generated to separate core
|
||||
extensions from standard libraries, see the [Learn Facets](http://rubyworks.github.com/facets/learn.html) page on the website for links to available documentation.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
### Bundler
|
||||
|
||||
If you are using Bundler with your project, add the facets gem to the project's
|
||||
Gemfile. Unless you want all of facets loaded be sure to add the `:require => false`
|
||||
option.
|
||||
|
||||
gem "facets", require: false
|
||||
|
||||
### RubyGems
|
||||
|
||||
The easiest way to install is via RubyGems.
|
||||
|
||||
$ gem install facets
|
||||
|
||||
### Setup.rb
|
||||
|
||||
Facets can be installed the old-fashioned way using [Setup.rb](http://rubyworks.github.com/setup).
|
||||
Download and unpack the .tar.gz package and run setup.rb, like so:
|
||||
|
||||
$ tar -xvzf facets-2.x.x.tar.gz
|
||||
$ cd facets-2.x.x
|
||||
$ sudo setup.rb
|
||||
|
||||
Facets 2.8+ requires Ruby 1.8.7 or higher. Facets 3.0+ requires Ruby 2.0.0 or higher.
|
||||
|
||||
|
||||
## Mission
|
||||
|
||||
Facets holds to the notion that the more we can *reasonably* integrate into
|
||||
a common foundation, directed toward general needs, the better that foundation
|
||||
will be able to serve the community. There are a number of advantages here:
|
||||
|
||||
* Better Code-reuse
|
||||
* Collaborative Improvements
|
||||
* Greater Name Consistency
|
||||
* One-stop Shop and Installation
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
### CORE Library
|
||||
|
||||
At the heart of Ruby Facets is the CORE extensions library. CORE provides
|
||||
a sizable collection of generally useful methods, along with a few supporting
|
||||
classes, that extend the functionality of Ruby's core classes and modules.
|
||||
|
||||
With the exception of a few *uncommon* extensions, CORE contains anything that
|
||||
will load automatically when issuing:
|
||||
|
||||
require 'facets'
|
||||
|
||||
This loads all the CORE functionality at once. If you plan to use more then a
|
||||
handful of Facets core methods it is recommended that you require the library in
|
||||
this way. However, you can also "cherry pick" the CORE library as you prefer.
|
||||
And for uncommon extensions this must be done. The general require statement for
|
||||
a core extension library is:
|
||||
|
||||
require 'facets/<class|module>/<method>'
|
||||
|
||||
For example:
|
||||
|
||||
require 'facets/time/stamp'
|
||||
|
||||
Most "atoms" contain only one method, but exceptions occur when methods
|
||||
are closely tied together.
|
||||
|
||||
You can load per-class or per-module groups of core methods by requiring the
|
||||
class or module by name. For example"
|
||||
|
||||
require 'facets/time'
|
||||
|
||||
Will require all the core Time method extensions.
|
||||
|
||||
Note that some methods that were part of CORE in 1.8 and earlier are now part
|
||||
of MORE libraries. A good example is 'random.rb'. There were separated because
|
||||
they had more specialized use cases, where as CORE extensions are intended as
|
||||
general purpose.
|
||||
|
||||
#### Method File Names
|
||||
|
||||
Operator method redirect files are stored using English names. For instance
|
||||
`Proc#*` is `proc/op_mul`.
|
||||
|
||||
For reference, here is the chart.
|
||||
|
||||
+@ => op_plus
|
||||
-@ => op_minus
|
||||
+ => op_add
|
||||
- => op_sub
|
||||
** => op_pow
|
||||
* => op_mul
|
||||
/ => op_div
|
||||
% => op_mod
|
||||
~ => op_tilde
|
||||
<=> => op_cmp
|
||||
<< => op_lshift
|
||||
>> => op_rshift
|
||||
< => op_lt
|
||||
> => op_gt
|
||||
=== => op_case
|
||||
== => op_equal
|
||||
=~ => op_apply
|
||||
<= => op_lt_eq
|
||||
>= => op_gt_eq
|
||||
| => op_or
|
||||
& => op_and
|
||||
^ => op_xor
|
||||
[]= => op_store
|
||||
[] => op_fetch
|
||||
|
||||
Facets simply takes the '*' and translates it into a string acceptable to all
|
||||
file systems. Also, if a method ends in '=', '?' or '!' it is simply removed.
|
||||
|
||||
|
||||
### MORE Library (aka Standard Library)
|
||||
|
||||
On top of the extensive CORE library, Facets provides extensions for Ruby's
|
||||
standard library, as well as a small collection of additional modules and
|
||||
classes to supplement it.
|
||||
|
||||
Use this library like you would any other 3rd party library.
|
||||
The only difference between Facet's Standard library and other libraries
|
||||
is the lack of any enclosing `Facets::` namespace.
|
||||
|
||||
When using Facets extended versions of Ruby's standard libraries,
|
||||
the libraries have to loaded individually. However you do not need
|
||||
to load Ruby's library first, as the Facets' library will do that
|
||||
automatically.
|
||||
|
||||
For example, normally one load Ruby's OpenStruct class via:
|
||||
|
||||
require 'ostruct'
|
||||
|
||||
To load 'ostruct.rb' plus Facets extensions for it simply use:
|
||||
|
||||
require 'facets/ostruct'
|
||||
|
||||
For details pertaining to the functionality of each feature,
|
||||
please see the API documentation.
|
||||
|
||||
|
||||
## Contribute
|
||||
|
||||
This project thrives on contribution!
|
||||
|
||||
If you have any extension methods, classes or modules that you think have
|
||||
very general applicability and would like to see them included in
|
||||
this project, don't hesitiate to submit. Also, if you have better versions
|
||||
of any thing already included or simply have a patch, they are more than
|
||||
welcome. We want Ruby Facets to be of the highest quality.
|
||||
|
||||
|
||||
## Development
|
||||
|
||||
Facets uses the [Lemon](http://rubyworks.github.com/lemon) testing framework
|
||||
to handle unit testing, while [QED](http://rubyworks.github.com/qed) specifications
|
||||
provide tested documentation.
|
||||
|
||||
Facets uses [Detroit](http://detroit.github.com) and [Rulebow](http://rubyworks.github.com/rulebow)
|
||||
build tools. Detroit is a life-cycle tool and Rulebow is continuous integrations tool. The build
|
||||
scripts (`Assembly` and `Rulebook` respectively), sometimes use other tools such as
|
||||
[Mast](http://rubyworks.github.com/mast) and [Indexer](http://rubyworks.github.com/indexer).
|
||||
|
||||
|
||||
## Authors
|
||||
|
||||
Much of this collection was written and/or inspired by a variety of great Ruby
|
||||
developers. Fortunately nearly all utilized works were copyrighted under the same
|
||||
open licenses, the Ruby License or the more liberal BSD and MIT licenses. In the
|
||||
one or two exceptions the copyright notice has been included with the source code.
|
||||
Any code file not specifically labeled othewise shall fall under the current Ruby
|
||||
License (which is BSD 2-clause).
|
||||
|
||||
In all cases, every effort has been made to give credit where credit is due.
|
||||
You will find these acknowledgments embedded in the source code. You can see
|
||||
them in "CREDIT:" and/or "@author" lines.
|
||||
Also see the [Contibutors page](https://github.com/rubyworks/facets/wiki/Contributors)
|
||||
on the Wiki for a list of all contributing Rubyists. If anyone is missing from
|
||||
the list, please let us know so we can correct right away. Thanks.
|
||||
|
||||
This collection was put together by, and much of it written by [trans](https://github.com/trans).
|
||||
If need be, he can be reached via email at transfire at gmail.com.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
The collection PER COLLECTION is licensed as follows:
|
||||
|
||||
Ruby Facets
|
||||
Copyright (c) 2004,2010 Rubyworks
|
||||
|
||||
Distributed under the terms of the Ruby license.
|
||||
|
||||
The Ruby license is a dual license that also provides for use of the GPL.
|
||||
Complete texts of both licenses accompany this document (see LICENSE).
|
||||
|
||||
Acknowledgments and Copyrights for particular snippets of borrowed code
|
||||
are given in their respective source. All licenses are either compatible
|
||||
with the Ruby license or the original author has given permission for
|
||||
inclusion of their code under such license.
|
||||
|
||||
|
||||
"ALL YOUR BASE ARE BELONG TO RUBY!"
|
||||
|
||||
Ruby Facets, Copyright (c)2005,2011 Rubyworks
|
||||
|
||||
Do you Ruby? (http://ruby-lang.org)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
require 'ae'
|
||||
@@ -0,0 +1,34 @@
|
||||
require 'fileutils'
|
||||
require 'pathname'
|
||||
|
||||
def temporary_directory
|
||||
@temporary_directory ||= 'tmp'
|
||||
end
|
||||
|
||||
Before :document do
|
||||
if File.exist?(temporary_directory)
|
||||
FileUtils.rm_r(temporary_directory)
|
||||
end
|
||||
FileUtils.mkdir(temporary_directory)
|
||||
end
|
||||
|
||||
When /Given a directory '(.*?)' containing/ do |dir, text|
|
||||
#abort unless /^#{temporary_directory}/ =~ dir
|
||||
text.lines.each do |file|
|
||||
next if file =~ /^\s*$/
|
||||
file = File.join(dir, file.strip)
|
||||
path = File.dirname(file)
|
||||
FileUtils.mkdir_p(path)
|
||||
File.open(file, 'w'){ |f| f << "SPINICH" }
|
||||
end
|
||||
end
|
||||
|
||||
# Return project root directory by looking for +lib+ directory.
|
||||
def root_directory
|
||||
@root_directory ||= (
|
||||
Pathname.new(Dir.pwd).ascend do |root|
|
||||
break root if root.join('lib').directory?
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#after
|
||||
|
||||
require 'facets/array/after'
|
||||
|
||||
`Array#after` returns the first value that comes after a given value. The
|
||||
value after the last is the first. Returns `nil` if the given value is not
|
||||
in the array.
|
||||
|
||||
sequence = ['a', 'b', 'c']
|
||||
sequence.after('a').assert == 'b'
|
||||
sequence.after('b').assert == 'c'
|
||||
sequence.after('c').assert == 'a'
|
||||
sequence.after('d').assert == nil
|
||||
@@ -0,0 +1,17 @@
|
||||
## Array#arrange
|
||||
|
||||
require 'facets/array/arrange'
|
||||
|
||||
The `arrange` method produces appropriate ranges from the objects in the array.
|
||||
It assumes inclusive ranges (i.e. `1..4`) and `range.first <= range.last`.
|
||||
|
||||
Works with integers, dates and strings. However, all the objects in the array must
|
||||
be of the same class.
|
||||
|
||||
[1,2,3,6,7,8].arrange.assert == [1..3, 6..8]
|
||||
|
||||
[10..15, 16..20, 21, 22].arrange.assert == [10..22]
|
||||
|
||||
a = %w{a b c g h i j k m o}
|
||||
a.arrange.assert == ['a'..'c','g'..'k', 'm' , 'o']
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#before
|
||||
|
||||
require 'facets/array/before'
|
||||
|
||||
Returns the value prior to the given value. The value previous
|
||||
to the first is the last. Returns nil if the given value is not
|
||||
in the array.
|
||||
|
||||
sequence = ['a', 'b', 'c']
|
||||
sequence.before('a').assert == 'c'
|
||||
sequence.before('b').assert == 'a'
|
||||
sequence.before('c').assert == 'b'
|
||||
sequence.before('d').assert == nil
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## Array#collapse
|
||||
|
||||
require 'facets/array/collapse'
|
||||
|
||||
Simplify an array by flattening it then compacting it.
|
||||
|
||||
[1,[2,nil,[3]],nil,4].collapse.assert == [1,2,3,4]
|
||||
|
||||
## Array#collapse!
|
||||
|
||||
Simplify an array by flattening it in place then compacting it in place.
|
||||
|
||||
a = [1, nil, [1, 2], nil, [[3]]]
|
||||
a.collapse!
|
||||
a.assert == [1, 1, 2, 3]
|
||||
@@ -0,0 +1,30 @@
|
||||
## Array#collisions
|
||||
|
||||
_alias for Array#commonality_.
|
||||
|
||||
require 'facets/array/collisions'
|
||||
|
||||
Get a list of all items that have something in common in terms of the
|
||||
supplied block. If no block is given objects are considered to be in
|
||||
common if they return the same value for Object#hash and if obj1 == obj2.
|
||||
|
||||
This can be useful, for instance, in determining all persons that share
|
||||
their last name with another person.
|
||||
|
||||
Person = Struct.new(:name)
|
||||
persons = %w{Wayne Pennyworth Grayson Wayne Pennyworth}.map{|n| Person.new(n)}
|
||||
persons.collisions { |person| person.name }.keys.assert == ["Wayne","Pennyworth"]
|
||||
|
||||
The method is similar to #group_by which is a standard Ruby method as of 1.9.
|
||||
To get effectively the same results with #group_by use `select{ |k,v| v.size > 1 }`.
|
||||
|
||||
expected = { 2 => [2, 2], 4 => [4, 4] }
|
||||
|
||||
[1, 2, 2, 3, 4, 4].group_by{ |e| e }.
|
||||
select{ |k,v| v.size > 1 }.assert == expected
|
||||
|
||||
[1, 2, 2, 3, 4, 4].collisions.assert == expected
|
||||
|
||||
%w{foo bar baz}.collisions{|str| str[0]}.assert == {'b' =>["bar", "baz"] }
|
||||
|
||||
Returns a Hash mapping common attribute to those elements.
|
||||
@@ -0,0 +1,31 @@
|
||||
## Array#commonality
|
||||
|
||||
require 'facets/array/commonality'
|
||||
|
||||
Get a list of all items that have something in common in terms of the
|
||||
supplied block. If no block is given objects are considered to be in
|
||||
common if they return the same value for Object#hash and if obj1 == obj2.
|
||||
|
||||
This can be useful, for instance, in determining all persons that share
|
||||
their last name with another person.
|
||||
|
||||
Person = Struct.new(:name)
|
||||
persons = %w{Wayne Pennyworth Grayson Wayne Pennyworth}.map{|n| Person.new(n)}
|
||||
persons.commonality { |person| person.name }.keys # => ["Wayne","Pennyworth"]
|
||||
|
||||
The method is similar to #group_by which is a standard Ruby method as of 1.9.
|
||||
To get effectively the same results with #group_by use `select{ |k,v| v.size > 1 }`.
|
||||
|
||||
expected = { 2 => [2, 2], 4 => [4, 4] }
|
||||
|
||||
[1, 2, 2, 3, 4, 4].group_by{ |e| e }.
|
||||
select{ |k,v| v.size > 1 }.assert == expected
|
||||
|
||||
[1, 2, 2, 3, 4, 4].commonality.assert == expected
|
||||
|
||||
|
||||
%w{foo bar baz}.commonality{|str| str[0]}.assert == {'b' =>["bar", "baz"] }
|
||||
|
||||
Returns a Hash mapping common attribute to those elements.
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
## Array#conjoin
|
||||
|
||||
require 'facets/array/conjoin'
|
||||
|
||||
This is more advanced form of #join. It allows for fine control
|
||||
of separators.
|
||||
|
||||
NOTE: The old version used to default its separator to ", " and
|
||||
default the terminating separator to " and ". This is no longer
|
||||
the case. You must specifically provide these parameters.
|
||||
|
||||
If no parameters are given, it acts like #join but will a space
|
||||
separator.
|
||||
|
||||
[1,2,3].conjoin.assert == "1 2 3"
|
||||
|
||||
Use comma+space and 'and' on tail.
|
||||
|
||||
[1,2,3].conjoin(', ', ' and ').assert == "1, 2 and 3"
|
||||
|
||||
Use comma+space and 'or' on tail using :last option.
|
||||
|
||||
[1,2,3].conjoin(', ', :last => ' or ').assert == "1, 2 or 3"
|
||||
|
||||
Use semicolon+space and ampersand on tail using index.
|
||||
|
||||
[1,2,3].conjoin('; ', -1 => ' & ').assert == "1; 2 & 3"
|
||||
|
||||
Can take a block to determine separator.
|
||||
|
||||
[1,2,3,4].conjoin{ |i, a, b| i % 2 == 0 ? '.' : '-' }.assert == "1.2-3.4"
|
||||
|
||||
This makes very esoteric transformation possible.
|
||||
|
||||
[1,1,2,2].conjoin{ |i, a, b| a == b ? '=' : ' != ' }.assert == "1=1 != 2=2"
|
||||
|
||||
[1,2,3,4].conjoin{ |i, x, y| "<#{i} #{x} #{y}>" }.assert == "1<0 1 2>2<1 2 3>3<2 3 4>4"
|
||||
|
||||
There are also spacing options. Providing the :space option
|
||||
pads the separators.
|
||||
|
||||
[1,2,3].conjoin(',', '&', :space=>2).assert == "1 , 2 & 3"
|
||||
|
||||
And the :spacer option can set an alternate spacing string.
|
||||
|
||||
[1,2,3].conjoin('|', '>', :space=>2, :spacer=>'-').assert == "1--|--2-->--3"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Array#contains?
|
||||
|
||||
_alias of Array#include? _
|
||||
|
||||
require 'facets/array/contains'
|
||||
|
||||
[1,2,3,4].assert.contains?(2)
|
||||
[1,2,3,4].refute.contains?(9)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#delete_unless
|
||||
|
||||
require 'facets/array/delete_unless'
|
||||
|
||||
The method `#delete_unless` is the inverse of `#delete_if`.
|
||||
|
||||
a = [1,2,3]
|
||||
a.delete_unless{ |e| e == 2 }
|
||||
a.assert == [2]
|
||||
|
||||
[1,2,3].delete_unless{ |x| x < 2 }.assert == [1]
|
||||
[1,2,3].delete_unless(&:even?).assert == [2]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#delete_values
|
||||
|
||||
require 'facets/array/delete_values'
|
||||
|
||||
Delete multiple values from array.
|
||||
|
||||
a = [1,2,3,4]
|
||||
a.delete_values(1,2) #=> [1,2]
|
||||
a #=> [3,4]
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## Array#delete_values_at
|
||||
|
||||
require 'facets/array/delete_values_at'
|
||||
|
||||
Delete multiple values from array given
|
||||
indexes or index range.
|
||||
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(1,2).assert == [2,3]
|
||||
a.assert == [1,4]
|
||||
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(0..2).assert == [1,2,3]
|
||||
a.assert == [4]
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#divide
|
||||
|
||||
require 'facets/array/divide'
|
||||
|
||||
Divide into sub arrays at elemetns matching given pattern
|
||||
|
||||
r = ['a1','b1','a2','b2'].divide(/^a/)
|
||||
r.assert == [['a1','b1'],['a2','b2']]
|
||||
|
||||
r = ['a1','b1','a2','b2'].divide(/^b/)
|
||||
r.assert == [['a1'],['b1','a2'],['b2']]
|
||||
|
||||
r = ['a1','b1','a2','b2'].divide(/c/)
|
||||
r.assert = [['a1','b1','a2','b2']]
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#duplicates
|
||||
|
||||
require 'facets/array/duplicates'
|
||||
|
||||
Return list of duplicate elements.
|
||||
|
||||
[1,1,2,3].duplicates #=> [1]
|
||||
|
||||
Return list of those duplicated min times.
|
||||
|
||||
[1,1,2,3,2,4,5,4,2].duplicates(3) #=> [2]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## Array#each_overlap
|
||||
|
||||
require 'facets/array/each_overlap'
|
||||
|
||||
Iterate over each slice where the last n values of a preceding slice
|
||||
overlap with the first n values of the following slice. The value of n
|
||||
is specified by the second `overlap` argument.
|
||||
|
||||
a, r = [1,2,3,4,5], []
|
||||
a.each_overlap(2,1) { |x,y| r << [x,y] }
|
||||
r.assert == [[1,2],[2,3],[3,4],[4,5]]
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#each_pair
|
||||
|
||||
require 'facets/array/each_pair'
|
||||
|
||||
Iterate over an array yielding with index and value.
|
||||
|
||||
result = {}
|
||||
a = [:a,:b,:c,:d]
|
||||
a.each_pair{|i,e| result[e] = i + 1}
|
||||
result.assert == {a: 1, b: 2, c: 3, d: 4}
|
||||
|
||||
*NOTE* this is the inverse of `Array#each_with_index`, and is intended to provide
|
||||
polymorphism with `Hash#each_pair`.
|
||||
@@ -0,0 +1,12 @@
|
||||
## Array#each_value
|
||||
|
||||
require 'facets/array/each_value'
|
||||
|
||||
This is an alias of Alias#each, and is provided to provide polymorphism with
|
||||
Hash#each_value.
|
||||
|
||||
a = [ "a", "b", "c" ]
|
||||
out = ''
|
||||
a.each_value {|x| out += "#{x} -- " }
|
||||
out.assert == 'a -- b -- c -- '
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#entropy
|
||||
|
||||
require 'facets/array/entropy'
|
||||
|
||||
Shannon's entropy for an array - returns the average
|
||||
bits per symbol required to encode the array.
|
||||
Lower values mean less "entropy" - i.e. less unique
|
||||
information in the array.
|
||||
|
||||
e = %w{ a b c d e e e }.entropy
|
||||
|
||||
("%.3f" % e) #=> "2.128"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#extract_options!
|
||||
|
||||
require 'facets/array/extract_options'
|
||||
|
||||
Extracts options from a set of arguments. Removes and returns the last
|
||||
element in the array if it's a hash, otherwise returns a blank hash.
|
||||
|
||||
def options(*args)
|
||||
args.extract_options!
|
||||
end
|
||||
|
||||
options(1, 2).assert == {}
|
||||
options(1, 2, :a => :b).assert == {:a=>:b}
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#first!
|
||||
|
||||
require 'facets/array/indexable'
|
||||
|
||||
Alias for shift, which removes and returns the first element in an array
|
||||
|
||||
a = ['a','y','z']
|
||||
a.first!.assert == 'a'
|
||||
a.assert ['y','z']
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#thru
|
||||
|
||||
require 'facets/array/from'
|
||||
|
||||
Returns elements from index i until the end.
|
||||
|
||||
%w{W o r l d}.from(3).assert == %w{l d}
|
||||
|
||||
Returns entire array for i > length
|
||||
|
||||
%w{W o r l d}.from(7).assert == nil
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#ideal_entropy
|
||||
|
||||
require 'facets/array/entropy'
|
||||
|
||||
Returns the maximum possible Shannon entropy of the array
|
||||
with given size assuming that it is an "order-0" source
|
||||
(each element is selected independently of the next).
|
||||
|
||||
%w{ a b }.ideal_entropy.assert == 1.0
|
||||
%w{ a a b b }.ideal_entropy.assert == 2.0
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#intersection
|
||||
|
||||
require 'facets/array/intersection'
|
||||
|
||||
Returns the values in common for an array set (nil, single value/object, or range).
|
||||
|
||||
[1,2].intersection.assert == nil
|
||||
[1..10,11..20].intersection.assert == nil
|
||||
[10,1..10].intersection.assert == 10
|
||||
[1..10,5,5..8,4..8].intersection.assert == 5
|
||||
[1..10, 5..8, 5..10 ].intersection.assert == (5..8)
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#last!
|
||||
|
||||
require 'facets/array/indexable'
|
||||
|
||||
Alias for pop, which removes and returns the last element in an
|
||||
array.
|
||||
|
||||
a = [1,2,3]
|
||||
a.last!.assert == 3
|
||||
a.assert == [1,2]
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Array#median
|
||||
|
||||
require 'facets/array/median'
|
||||
|
||||
Determines the _sorted_ middle element.
|
||||
|
||||
a = %w{a a b b c c c}
|
||||
a.median #=> "b"
|
||||
|
||||
When there are an even number of elements, the greater
|
||||
of the two middle elements is given.
|
||||
|
||||
a = %w{a a b b c c c d}
|
||||
a.median #=> "c"
|
||||
|
||||
An offset can be supplied to get an element relative
|
||||
to the middle.
|
||||
|
||||
a = %w{a a b b c c c d}
|
||||
a.median(-1) #=> "b"
|
||||
|
||||
The the array is empty, +nil+ is returned.
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## Array#merge
|
||||
|
||||
require 'facets/array/merge'
|
||||
|
||||
a = [1,2,3]
|
||||
b = [3,4,5]
|
||||
a.merge(b).assert == [1,2,3,4,5]
|
||||
|
||||
## Array#merge!
|
||||
|
||||
a = [1,2,3]
|
||||
b = [3,4,5]
|
||||
a.merge!(b)
|
||||
a.assert == [1,2,3,4,5]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#missing
|
||||
|
||||
require 'facets/array/missing'
|
||||
|
||||
Determine the 'holes' in the values of an array. Returns the missing elements
|
||||
in an array set.
|
||||
|
||||
[1,3..3].missing.assert == [2]
|
||||
[1..5,10..12].missing.assert == [6..9]
|
||||
[100, 9..11, 14, 1..5, 16, 10..12, 17..17].missing.assert == [6..8, 13, 15, 18..99]
|
||||
@@ -0,0 +1,9 @@
|
||||
## Array#mode
|
||||
|
||||
require 'facets/array/mode'
|
||||
|
||||
In Statistics, mode is the value that occurs most frequently in a given set of
|
||||
data. This method returns an array in case there is a tie.
|
||||
|
||||
[1, 1, 2, 3].mode.assert == [1]
|
||||
[1, 1, 2, 2, 3].mode.assert == [1,2]
|
||||
@@ -0,0 +1,16 @@
|
||||
## Array#nonuniq
|
||||
|
||||
require 'facets/array/nonuniq'
|
||||
|
||||
Returns a list of non uniq elements.
|
||||
|
||||
a = [1, 1, 2, 2, 3, 4, 5]
|
||||
a.nonuniq.sort.assert == [1, 2]
|
||||
|
||||
## Array#nonuniq!
|
||||
|
||||
Same as #nonuniq but acts in place.
|
||||
|
||||
a = [1, 1, 2, 2, 3, 4, 5]
|
||||
a.nonuniq!
|
||||
a.sort.assert == [1,2]
|
||||
@@ -0,0 +1,8 @@
|
||||
## Array#not_empty?
|
||||
|
||||
require 'facets/array/not_empty'
|
||||
|
||||
Array is not empty?
|
||||
|
||||
[1,2].assert.not_empty?
|
||||
[].refute.not_empty?
|
||||
@@ -0,0 +1,18 @@
|
||||
## Array#occur
|
||||
|
||||
require 'facets/array/occur'
|
||||
|
||||
Returns a list of elements that occur +n+ times.
|
||||
|
||||
If +n+ is a Range then returns elements that occur a number
|
||||
of time within the range.
|
||||
|
||||
a = [:a,:b,:a]
|
||||
a.occur(1).assert == [:b]
|
||||
a.occur(2).assert == [:a]
|
||||
|
||||
a = [:a,:b,:a]
|
||||
a.occur(1..2).assert == [:a,:b]
|
||||
|
||||
a = [:a,:b,:a]
|
||||
a.occur{ |n| n % 2 == 0 }.assert == [:a]
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#occurrence
|
||||
|
||||
require 'facets/array/occurrence'
|
||||
|
||||
Create a hash of each uniq element of the array
|
||||
and how many time each appears.
|
||||
|
||||
r = [:a,:a,:b,:c,:c,:c].occurrence
|
||||
r.assert == { :a => 2, :b => 1, :c => 3 }
|
||||
|
||||
r = [2,2,3,4,4,4].occurrence{|i| i % 2}
|
||||
r.assert == { 0 => 5, 1 => 1 }
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Array#only
|
||||
|
||||
require 'facets/array/only'
|
||||
|
||||
[5].only.assert == 5
|
||||
[nil].only.assert == nil
|
||||
expect(IndexError){ [].only }
|
||||
expect(IndexError){ [1,2,3].only }
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#**
|
||||
|
||||
require 'facets/array/op_pow'
|
||||
|
||||
Alias for Array#product
|
||||
|
||||
NOTE: This method is not a common core extension and is not loaded automatically
|
||||
when using ```require 'facets'```
|
||||
|
||||
([1,2] ** [3,4]).assert == [[1, 3], [1, 4], [2, 3], [2, 4]]
|
||||
@@ -0,0 +1,28 @@
|
||||
## Array#pad
|
||||
|
||||
require 'facets/array/pad'
|
||||
|
||||
r = [0,1,2,3].pad(7,"x")
|
||||
x = [0,1,2,3,"x","x","x"]
|
||||
r.assert == x
|
||||
|
||||
negative index
|
||||
|
||||
r = [0,1,2,3].pad(-7,"n")
|
||||
x = ["n","n","n",0,1,2,3]
|
||||
r.assert == x
|
||||
|
||||
## Array#pad!
|
||||
|
||||
a = [0,1,2,3]
|
||||
r = a.pad!(6,"y")
|
||||
x = [0,1,2,3,"y","y"]
|
||||
r.assert == x
|
||||
|
||||
negative index
|
||||
|
||||
a = [0,1,2,3]
|
||||
r = a.pad!(-6,"q")
|
||||
x = ["q","q",0,1,2,3]
|
||||
r.assert == x
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#peek
|
||||
|
||||
require 'facets/array/peek'
|
||||
|
||||
Provide an index to inspect the array from back to front.
|
||||
|
||||
[1,2,3].peek .assert == 3
|
||||
[1,2,3].peek(1) .assert == 2
|
||||
[1,2,3].peek(-1).assert == 1
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
## Array#poke
|
||||
|
||||
require 'facets/array/poke'
|
||||
|
||||
Put an object on the bottom of the stack (front of the array).
|
||||
|
||||
a = [2,3]
|
||||
a.poke(1)
|
||||
a.assert == [1,2,3]
|
||||
|
||||
Or supply an index and #poke works like insert.
|
||||
|
||||
a = [1,3]
|
||||
a.poke(2,1)
|
||||
a.assert == [1,2,3]
|
||||
@@ -0,0 +1,8 @@
|
||||
## Array#probability
|
||||
|
||||
require 'facets/array/probability'
|
||||
|
||||
Generates a hash mapping each unique element in the array to the relative
|
||||
frequency, i.e. the probability, of it's appearance.
|
||||
|
||||
[:a, :b, :c, :c].probability.assert == {a: 0.25, b: 0.25, c: 0.50}
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#pull
|
||||
|
||||
require 'facets/array/pull'
|
||||
|
||||
Alias for Array#shift which removes an object off the first slot of an array.
|
||||
This is the opposite of pop.
|
||||
|
||||
a = [1,2,3]
|
||||
a.pull.assert == 1
|
||||
a.assert == [2,3]
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#recurse
|
||||
|
||||
require 'facets/array/recurse'
|
||||
|
||||
Apply a block to array, and recursively apply that block to each sub-array
|
||||
or +type+.
|
||||
|
||||
a = ["a", ["b", "c", nil], nil]
|
||||
r = a.recurse{|a| a.compact!}
|
||||
r.assert == ["a", ["b", "c"]]
|
||||
@@ -0,0 +1,20 @@
|
||||
## Array#recursively
|
||||
|
||||
require 'facets/array/recursively'
|
||||
|
||||
Apply a method to array, and recursively apply that method to each sub-array
|
||||
or given +types+.
|
||||
|
||||
By default the sub-types are passed through unaffected. Passing a block
|
||||
to #recursively can be used to change this.
|
||||
|
||||
each
|
||||
|
||||
r = []
|
||||
[1,2,['a','b'], 3].recursively.each{|v| r << v}
|
||||
r.assert = [1,2,'a','b', 3]
|
||||
|
||||
map
|
||||
|
||||
arr = ['foo','bar',['a','b']]
|
||||
arr.recursively.map{|v| v.to_sym}.assert == [:foo,:bar,[:a,:b]]
|
||||
@@ -0,0 +1,10 @@
|
||||
## Array#reject_values
|
||||
|
||||
require 'facets/array/reject_values'
|
||||
|
||||
Non-destructive form of Array#delete_values. Unlike delete_values this method
|
||||
returns a new array.
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.reject_values(2,4).assert == [1,3,5]
|
||||
a.assert [1,2,3,4,5]
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#splice
|
||||
|
||||
require 'facets/array/splice'
|
||||
|
||||
Splice acts as a combination of #slice! and #store. If one argument is given it
|
||||
calls #slice!, if two are given it calls #store.
|
||||
|
||||
a = [1,2,3]
|
||||
a.splice(1).assert == 2
|
||||
a.assert == [1,3]
|
||||
|
||||
b = [1,2,3]
|
||||
b.splice(1,4).assert == 4
|
||||
b.assert == [1,4,3]
|
||||
@@ -0,0 +1,13 @@
|
||||
## Array#split
|
||||
|
||||
require 'facets/array/split'
|
||||
|
||||
Split on matching pattern. Unlike #divide, this does not include matching
|
||||
elements.
|
||||
|
||||
['a','b','c'].split('b').assert == [['a'],['c']]
|
||||
|
||||
a = ['a1','a2','b1','a3','b2','a4']
|
||||
a.split(/^b/).assert == [['a1','a2'],['a3'],['a4']]
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#squeeze!
|
||||
|
||||
require 'facets/array/squeeze'
|
||||
|
||||
Destructive version of Enumerable#squeeze.
|
||||
|
||||
a = [1,2,2,3,3,2,1]
|
||||
a.squeeze!
|
||||
a.assert == [1,2,3,2,1]
|
||||
|
||||
a = [1,2,2,3,3,2,1]
|
||||
a.squeeze!(3)
|
||||
a.assert == [1,2,2,3,2,1]
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Array#step
|
||||
|
||||
require 'facets/array/step'
|
||||
|
||||
Iterate over every nth element of an array. Without a block, it returns an
|
||||
Enumerator.
|
||||
|
||||
r = []
|
||||
[:a, :b, :c, :d].step(2) { |x| r << x }
|
||||
r.assert == [:b, :d]
|
||||
|
||||
[:a, :b, :c, :d].step(1).to_a.assert == [:a, :b, :c, :d]
|
||||
[:a, :b, :c, :d].step(2).to_a.assert == [:b, :d]
|
||||
[:a, :b, :c, :d].step(3).to_a.assert == [:c]
|
||||
[:a, :b, :c, :d].step(4).to_a.assert == [:d]
|
||||
[:a, :b, :c, :d].step(5).to_a.assert == []
|
||||
@@ -0,0 +1,9 @@
|
||||
## Array#store
|
||||
|
||||
require 'facets/array/store'
|
||||
|
||||
Store a value at a given index. Store is an alias for #[]=
|
||||
|
||||
a = []
|
||||
a.store(1,"A")
|
||||
a[1].assert == "A"
|
||||
@@ -0,0 +1,12 @@
|
||||
## Array#thru
|
||||
|
||||
require 'facets/array/thru'
|
||||
|
||||
Fetch Values from a start index thru an end index.
|
||||
|
||||
[1,2,3,4,5].thru(0,2) #=> [1,2,3]
|
||||
[1,2,3,4,5].thru(2,4) #=> [3,4,5]
|
||||
|
||||
[1,2,3,4,5].thru(2) #=> [1,2,3]
|
||||
[1,2,3,4,5].thru(4) #=> [1,2,3,4,5]
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Array#to_h
|
||||
|
||||
require 'facets/array/to_h'
|
||||
|
||||
Convert an associative array to a Hash. Each element of the associative array
|
||||
should be a 1 or 2 element array
|
||||
|
||||
Note this is built into ruby as of 2.1.0
|
||||
|
||||
arr = [[:a, 1], [:b, 2]]
|
||||
arr.to_h.assert == {:a => 1, :b => 2}
|
||||
@@ -0,0 +1,20 @@
|
||||
## Array#traverse
|
||||
|
||||
require 'facets/array/traverse'
|
||||
|
||||
Construct a new array created by traversing the array and its sub-arrays,
|
||||
executing the given block on the elements.
|
||||
|
||||
h = ['A', 'B', ['X', 'Y']]
|
||||
g = h.traverse{ |e| e.downcase }
|
||||
g.assert = ['a','b',['x', 'y']]
|
||||
|
||||
## Array#traverse!
|
||||
|
||||
Like #traverse, but will change the array in place.
|
||||
|
||||
h = ['A', 'B', ['X', 'Y']]
|
||||
h.traverse!{ |e| e.downcase }
|
||||
h.assert = ['a','b',['x', 'y']]
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Array#uniq_by!
|
||||
|
||||
require 'facets/array/uniq_by'
|
||||
|
||||
Like #uniq, but determines uniqueness based on a given block.
|
||||
As can be seen from the examples, order is significant.
|
||||
|
||||
r = (-5..5).to_a
|
||||
r.uniq_by!{ |i| i*i }
|
||||
r.assert == [-5, -4, -3, -2, -1, 0]
|
||||
|
||||
r = (-5..5).to_a.reverse
|
||||
r.uniq_by!{ |i| i*i }
|
||||
r.assert == [5, 4, 3, 2, 1, 0]
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
## Array#unique_permutation
|
||||
|
||||
require 'facets/array/unique_permutation'
|
||||
|
||||
Enumerates permutation of Array. Unlike Array#permutation, there are no
|
||||
duplicates in generated permutations. Instead elements must be comparable
|
||||
|
||||
p = [1,1,2,2,3].unique_permutation(2).to_a
|
||||
e = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2]]
|
||||
p.assert == e
|
||||
|
||||
[1,1,2,3].unique_permutation.to_a.assert != [1,1,2,3].permutation.to_a
|
||||
|
||||
[1,1,2,3].unique_permutation.to_a.assert == [1,1,2,3].permutation.to_a.uniq
|
||||
@@ -0,0 +1,8 @@
|
||||
## Array::zip
|
||||
|
||||
require 'facets/array/zip'
|
||||
|
||||
Class level rendition of Array#zip
|
||||
|
||||
Array.zip([1,2], [3,4]).assert == [[1,3],[2,4]]
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
The caller.rb script includes a few related methods.
|
||||
We will use the following code to demonstrate their use.
|
||||
|
||||
require 'facets/binding/caller'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
@bind = binding
|
||||
@line = __LINE__ # the line number must be updated if it moves
|
||||
@file = __FILE__ # why does it equal basename only?
|
||||
|
||||
## Binding#caller
|
||||
|
||||
@bind.caller
|
||||
|
||||
## Binding#callstack
|
||||
|
||||
@bind.callstack.assert.is_a?(Array)
|
||||
|
||||
## Binding#__LINE__
|
||||
|
||||
@bind.__LINE__.assert == @line - 1
|
||||
|
||||
## Binding#__FILE__
|
||||
|
||||
@bind.__FILE__.assert == @file
|
||||
|
||||
## Binding#__DIR__
|
||||
|
||||
@bind.__DIR__.assert == File.dirname(@file)
|
||||
|
||||
## Binding#__callee__
|
||||
|
||||
This only works for certain versions.
|
||||
|
||||
def alternate_callee
|
||||
binding
|
||||
end
|
||||
|
||||
unless RUBY_VERSION < "1.9"
|
||||
alternate_callee.__callee__.assert == :alternate_callee
|
||||
end
|
||||
|
||||
## Binding#__method__
|
||||
|
||||
This only works for certain versions.
|
||||
|
||||
def alternate_method
|
||||
binding
|
||||
end
|
||||
|
||||
unless RUBY_VERSION < "1.8.7"
|
||||
alternate_method.__method__.assert == :alternate_method
|
||||
end
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Binding#defined?
|
||||
|
||||
require 'facets/binding/defined'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
binding.assert.defined?("x")
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Binding#[]
|
||||
|
||||
require 'facets/binding/op'
|
||||
|
||||
a = 1
|
||||
b = 2
|
||||
x = "hello"
|
||||
|
||||
binding["x"].assert == "hello"
|
||||
|
||||
## Binding#[]=
|
||||
|
||||
binding["x"] = "goodbye"
|
||||
|
||||
binding["x"].assert == "goodbye"
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
## Binding#self
|
||||
|
||||
require 'facets/binding/self'
|
||||
|
||||
binding.self == self
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## Class#descendants
|
||||
|
||||
require 'facets/class/descendants'
|
||||
|
||||
The `Class#descendants` method returns a list of classes that are subclasses
|
||||
of a class.
|
||||
|
||||
a = Class.new
|
||||
b = Class.new(a)
|
||||
c = Class.new(b)
|
||||
|
||||
a.descendants.assert == [b,c]
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Class#to_proc
|
||||
|
||||
require 'facets/class/to_proc'
|
||||
|
||||
The `Class#to_proc` method provides a convenient way to create new
|
||||
instances from a set of initialization data.
|
||||
|
||||
person = Struct.new(:name)
|
||||
people = ["joe"].map(&person)
|
||||
people[0].name.assert == "joe"
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Comparable#at_least
|
||||
|
||||
require 'facets/comparable/at_least'
|
||||
|
||||
Limits a number to at least a given amount.
|
||||
|
||||
3.at_least(4).assert == 4
|
||||
4.at_least(4).assert == 4
|
||||
5.at_least(4).assert == 5
|
||||
|
||||
This is similar to `#clip`.
|
||||
@@ -0,0 +1,11 @@
|
||||
## Comparable#at_most
|
||||
|
||||
require 'facets/comparable/at_most'
|
||||
|
||||
Limits a number to at most a given amount.
|
||||
|
||||
3.at_most(4).assert == 3
|
||||
4.at_most(4).assert == 4
|
||||
5.at_most(4).assert == 4
|
||||
|
||||
This is similar to `#cap`.
|
||||
@@ -0,0 +1,13 @@
|
||||
## Comparable#bound
|
||||
|
||||
require 'facets/comparable/bound'
|
||||
|
||||
The `#bound` method is an alias for `#clip`. It is the original name
|
||||
of the method before the `clip`/`cap` duo was decided upon.
|
||||
|
||||
3.bound(4).assert == 4
|
||||
|
||||
4.bound(3,5).assert == 4
|
||||
|
||||
'd'.bound('c','e').assert == 'd'
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Comparable#cap
|
||||
|
||||
require 'facets/comparable/cap'
|
||||
|
||||
Put a cap on what a number can be.
|
||||
|
||||
3.cap(4).assert == 3
|
||||
4.cap(4).assert == 4
|
||||
5.cap(4).assert == 4
|
||||
|
||||
This is very similar to `#at_most`.
|
||||
@@ -0,0 +1,29 @@
|
||||
## Comparable#clip
|
||||
|
||||
require 'facets/comparable/clip'
|
||||
|
||||
Given a single argument `#clip` behaves like `#at_least`.
|
||||
|
||||
3.clip(4).assert == 4
|
||||
4.clip(4).assert == 4
|
||||
5.clip(4).assert == 5
|
||||
|
||||
With two arguments `#clip` puts both a lower and an upper limit
|
||||
on the return value.
|
||||
|
||||
4.clip(3,5).assert == 4
|
||||
3.clip(3,5).assert == 3
|
||||
5.clip(3,5).assert == 5
|
||||
2.clip(3,5).assert == 3
|
||||
6.clip(3,5).assert == 5
|
||||
|
||||
This almost works on characters (using ASCII order).
|
||||
|
||||
'd'.clip('c','e').assert == 'd'
|
||||
'c'.clip('c','e').assert == 'c'
|
||||
'e'.clip('c','e').assert == 'e'
|
||||
'b'.clip('c','e').assert == 'c'
|
||||
'f'.clip('c','e').assert == 'e'
|
||||
|
||||
This method is aliases as `bound`.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
## Comparable#cmp
|
||||
|
||||
require 'facets/comparable/cmp'
|
||||
|
||||
On integers
|
||||
|
||||
3.cmp(4).assert == -1
|
||||
3.cmp(3).assert == 0
|
||||
3.cmp(2).assert == 1
|
||||
|
||||
On strings
|
||||
|
||||
"abc".cmp("abc").assert == 0
|
||||
"abc".cmp("abcd").assert == -1
|
||||
"abcd".cmp("abc").assert == 1
|
||||
"abc".cmp("bcd").assert == -1
|
||||
"bcd".cmp("abc").assert == 1
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
## Comparable.[]
|
||||
|
||||
require 'facets/comparable/op_get'
|
||||
|
||||
Allows `Comparable` to be included as a *parmetric mixin*. This makes it easy to define
|
||||
a class as comparable on a limited set of attributes.
|
||||
|
||||
c = Class.new do
|
||||
include Comparable[:a,:b]
|
||||
|
||||
attr_accessor :a, :b
|
||||
|
||||
def initialize(a,b)
|
||||
@a=a; @b=b
|
||||
end
|
||||
end
|
||||
|
||||
a = [c.new(10,20),c.new(10,30)]
|
||||
a.sort.assert == a
|
||||
|
||||
a = [c.new(10,30),c.new(10,20)]
|
||||
a.sort.assert == a.reverse
|
||||
|
||||
a = [c.new(10,10),c.new(20,10)]
|
||||
a.sort.assert == a
|
||||
|
||||
a = [c.new(20,10),c.new(10,10)]
|
||||
a.sort.assert == a.reverse
|
||||
|
||||
a = [c.new(10,30),c.new(20,10)]
|
||||
a.sort.assert == a
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
## Dir#ascend
|
||||
|
||||
require 'facets/dir/ascend'
|
||||
|
||||
path = 'A/B/C'
|
||||
|
||||
tmp = []
|
||||
|
||||
Dir.ascend(path) do |d|
|
||||
tmp << d
|
||||
end
|
||||
|
||||
tmp.assert == %w{A/B/C A/B A}
|
||||
|
||||
Exclude current path.
|
||||
|
||||
tmp = []
|
||||
|
||||
Dir.ascend(path, false) do |d|
|
||||
tmp << d
|
||||
end
|
||||
|
||||
tmp.assert == %w{A/B A}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
## Dir#descend
|
||||
|
||||
require 'facets/dir/descend'
|
||||
|
||||
Descend thru a file path.
|
||||
|
||||
path = 'A/B/C'
|
||||
|
||||
tmp = []
|
||||
|
||||
Dir.descend(path) do |d|
|
||||
tmp << d
|
||||
end
|
||||
|
||||
tmp.assert == %w{A A/B A/B/C}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
## Dir#multiglob
|
||||
|
||||
Dir#multglob is like Dir#glob but it can handle more than one match
|
||||
parameter at a time.
|
||||
|
||||
Given a directory 'multiglob' containing:
|
||||
|
||||
A.txt
|
||||
A/B.txt
|
||||
A/B/C.txt
|
||||
|
||||
We can use #multglob to find multiple matches.
|
||||
|
||||
require 'facets/dir/multiglob'
|
||||
|
||||
x = %w{
|
||||
multiglob/A
|
||||
multiglob/A.txt
|
||||
}
|
||||
|
||||
r = Dir.multiglob('multiglob/A', 'multiglob/A.*').sort
|
||||
|
||||
r.assert == x
|
||||
|
||||
## Dir#multiglob_r
|
||||
|
||||
Dir#multiglob_r is like #multiglob but automatically searches
|
||||
directories recrusively.
|
||||
|
||||
x = %w{
|
||||
multiglob/A
|
||||
multiglob/A/B
|
||||
multiglob/A.txt
|
||||
multiglob/A/B.txt
|
||||
multiglob/A/B/C.txt
|
||||
}.sort
|
||||
|
||||
r = Dir.multiglob_r('multiglob/*').sort
|
||||
|
||||
r.assert == x
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
## Dir#parent
|
||||
|
||||
require 'facets/dir/parent'
|
||||
|
||||
Dir.assert.parent?("a/b/c", "a/b/c/d")
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
## Dir#recurse
|
||||
|
||||
Given a directory 'recurse' containing:
|
||||
|
||||
A.txt
|
||||
A/B.txt
|
||||
A/B/C.txt
|
||||
|
||||
We can use #recurse to gather a complete recursive
|
||||
list of all the entries.
|
||||
|
||||
require 'facets/dir/recurse'
|
||||
|
||||
x = %w{
|
||||
recurse/A
|
||||
recurse/A.txt
|
||||
recurse/A/B
|
||||
recurse/A/B.txt
|
||||
recurse/A/B/C.txt
|
||||
}.sort
|
||||
|
||||
r = Dir.recurse('recurse').sort
|
||||
|
||||
r.assert == x
|
||||
|
||||
## Dir#ls_r
|
||||
|
||||
Dir#ls_r is an alias for #recurse which can also be used to gather
|
||||
a complete recursive list of all the entries.
|
||||
|
||||
x = %w{
|
||||
recurse/A
|
||||
recurse/A.txt
|
||||
recurse/A/B
|
||||
recurse/A/B.txt
|
||||
recurse/A/B/C.txt
|
||||
}.sort
|
||||
|
||||
r = Dir.ls_r('recurse').sort
|
||||
|
||||
r.assert == x
|
||||
@@ -0,0 +1,19 @@
|
||||
## Exception#detail
|
||||
|
||||
require 'facets/exception/detail'
|
||||
|
||||
Provides a more detailed error message.
|
||||
|
||||
begin
|
||||
raise ArgumentError
|
||||
rescue ArgumentError => err
|
||||
r = err.detail
|
||||
r.assert.include?('ArgumentError')
|
||||
end
|
||||
|
||||
The output of `#detail` looks something like this:
|
||||
|
||||
ArgumentError: ArgumentError
|
||||
example.rb:4:in `<main>'
|
||||
LOGGED FROM: example.rb:6:in `rescue in <main>'
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# Requiring Facets
|
||||
|
||||
We should be able to require all the core libraries by simply calling,
|
||||
|
||||
require 'facets'
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
## FileTest#root?
|
||||
|
||||
require 'facets/filetest/root'
|
||||
|
||||
Returns true if the given directory is the file system root.
|
||||
Obviously on a Linux system them means `/`.
|
||||
|
||||
FileTest.assert.root?('/')
|
||||
|
||||
Anything else will return false.
|
||||
|
||||
FileTest.refute.root?('/home')
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
## Hash#recursively
|
||||
|
||||
require 'facets/hash/recursively'
|
||||
|
||||
h = {:a=>1, :b=>{:c=>3}}
|
||||
|
||||
Without a block passed to the `#recursively` method, the block passed to the
|
||||
enumeratig method will be used for both recursive and non-recursive yields.
|
||||
|
||||
r = h.recursively.map{ |k,v| [k.to_s, v] }
|
||||
|
||||
r.assert == [['a', 1], ['b', [['c', 3]]]]
|
||||
|
||||
But if we pass a block to `#recursively` we can treat the recusive yield
|
||||
separately, which in turn allows us to manipulate the non-recursive
|
||||
yields with a different procedure.
|
||||
|
||||
r = h.recursively{ |k,v| [k.to_s, v] }.map{ |k,v| [k.to_s,v.to_s] }
|
||||
|
||||
r.assert == [['a','1'], ['b', [['c', '3']]]]
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
# Indexable
|
||||
|
||||
Indexable it a module, like Enumerable, but provides methods specific
|
||||
to an indexable collection. The Indexable module can be used independently,
|
||||
but Facets can also automatically use it to extend Array.
|
||||
|
||||
require 'facets/array/indexable'
|
||||
|
||||
## Indexable#head
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.head.assert = [1]
|
||||
|
||||
## Indexable#tail
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.tail.assert = [2,3,4,5]
|
||||
|
||||
## Indexable#foot
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.foot.assert = [5]
|
||||
|
||||
## Indexable#body
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.body.assert = [1,2,3,4]
|
||||
|
||||
## Indexable#mid
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
b = [1,2,3,4,5,6]
|
||||
a.mid.assert = 3
|
||||
b.mid.assert = 4
|
||||
a.mid(1).assert = 4
|
||||
b.mid(1).assert = 5
|
||||
b.mid(2).assert = 6
|
||||
b.mid(-1).assert = 3
|
||||
|
||||
## Indexable#middle
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
b = [1,2,3,4,5,6]
|
||||
a.middle.assert = [3]
|
||||
b.middle.assert = [3,4]
|
||||
|
||||
## Indexable#thru
|
||||
|
||||
[0,1,2,3,4,5].thru(2,4).assert = [2,3,4]
|
||||
[0,1,2,3,4,5].thru(0,1).assert = [0,1]
|
||||
|
||||
## Indexable#first=
|
||||
|
||||
a = [1,2]
|
||||
a.first = 0
|
||||
a.assert = [0,2]
|
||||
|
||||
## Indexable#last=
|
||||
|
||||
a = [1,2]
|
||||
a.last = 3
|
||||
a.assert = [1,3]
|
||||
|
||||
## Indexable#ends
|
||||
|
||||
[1,2,3,4,5].ends.assert = 4
|
||||
|
||||
## Indexable#pos
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
a.pos(1).assert = 0
|
||||
a.pos(-1).assert = 4
|
||||
|
||||
## Indexable#range
|
||||
|
||||
a = [1,2,3,4,5]
|
||||
b = [1,2,3,4,5,6]
|
||||
a.range.assert = (0..4)
|
||||
b.range.assert = (0..5)
|
||||
a.range(2,4).assert = (1..3)
|
||||
b.range(2,3).assert = (1..2)
|
||||
b.range(4,2).assert = (3..1)
|
||||
|
||||
## Indexable#first!
|
||||
|
||||
a = [1,2,3]
|
||||
a.first!.assert = 1
|
||||
a.assert = [2,3]
|
||||
|
||||
## Indexable#last!
|
||||
|
||||
a = [1,2,3]
|
||||
a.last!.assert = 3
|
||||
a.assert = [1,2]
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
## Integer#bit
|
||||
|
||||
require 'facets/integer/bitmask'
|
||||
|
||||
0.bit(0).assert == 1
|
||||
0.bit(1).assert == 2
|
||||
0.bit(2).assert == 4
|
||||
0.bit(3).assert == 8
|
||||
|
||||
Negate
|
||||
|
||||
1.bit(~0).assert == 0
|
||||
2.bit(~1).assert == 0
|
||||
4.bit(~2).assert == 0
|
||||
8.bit(~3).assert == 0
|
||||
|
||||
## Integer#bit?
|
||||
|
||||
a = 8
|
||||
assert(! a.bit?(0))
|
||||
assert(! a.bit?(1))
|
||||
assert(! a.bit?(2))
|
||||
assert( a.bit?(3))
|
||||
assert(! a.bit?(4))
|
||||
assert(! a.bit?(5))
|
||||
|
||||
## Integer#bit_clear
|
||||
|
||||
1.bit_clear(0).assert == 0
|
||||
2.bit_clear(1).assert == 0
|
||||
4.bit_clear(2).assert == 0
|
||||
8.bit_clear(3).assert == 0
|
||||
|
||||
## Integer#bitmask
|
||||
|
||||
a = 1
|
||||
m = Bit(4)
|
||||
a = a.bitmask(m)
|
||||
a.assert == 17
|
||||
assert( a.bitmask?(m) )
|
||||
|
||||
## Kernel#Bit
|
||||
|
||||
n = Bit(4)
|
||||
n.assert == 16
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Integer#factorial
|
||||
|
||||
require 'facets/integer/factorial'
|
||||
|
||||
0.factorial.assert == 1
|
||||
1.factorial.assert == 1
|
||||
2.factorial.assert == 2
|
||||
3.factorial.assert == 6
|
||||
4.factorial.assert == 24
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Numeric#length
|
||||
|
||||
require 'facets/numeric/length'
|
||||
|
||||
10.length.assert == 10
|
||||
10.0.length.assert == 10
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## Integer#multiple?
|
||||
|
||||
require 'facets/integer/multiple'
|
||||
|
||||
Is a number an even multiple of another?
|
||||
|
||||
1.multiple?(2) #=> false
|
||||
5.multiple?(3) #=> false
|
||||
|
||||
2.multiple?(2) #=> true
|
||||
6.multiple?(3) #=> true
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Integer#of
|
||||
|
||||
require 'facets/integer/of'
|
||||
|
||||
Similar to `Enumerable#map` and `Enumerable#collect` but for a number
|
||||
of times.
|
||||
|
||||
a = 4
|
||||
b = a.of{ |i| i*2 }
|
||||
b.assert == [0,2,4,6]
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Integer#ordinal
|
||||
|
||||
require 'facets/integer/ordinal'
|
||||
|
||||
Produce the ordinal name of a number.
|
||||
|
||||
1.ordinal.assert == '1st'
|
||||
2.ordinal.assert == '2nd'
|
||||
3.ordinal.assert == '3rd'
|
||||
4.ordinal.assert == '4th'
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Integer#times_collect
|
||||
|
||||
require 'facets/integer/of'
|
||||
|
||||
a = 4
|
||||
b = a.times_collect{ |i| i*2 }
|
||||
b.assert == [0,2,4,6]
|
||||
|
||||
See `Integer#of` which is an alias.
|
||||
@@ -0,0 +1,7 @@
|
||||
## MatchData#match
|
||||
|
||||
require 'facets/matchdata/match'
|
||||
|
||||
md = /X(a)(b)(c)X/.match("YXabcXY")
|
||||
md.match.assert == "XabcX"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## MatchData#matchset
|
||||
|
||||
require 'facets/matchdata/matchset'
|
||||
|
||||
md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX"
|
||||
md.matchset.assert == ["XXaa", [["bb"], ["cc", ["dd"]], ["ee"]], "ffXX"]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## MatchData#matchtree
|
||||
|
||||
require 'facets/matchdata/matchtree'
|
||||
|
||||
md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX"
|
||||
md.matchtree.assert == [["bb"], ["cc", ["dd"]], ["ee"]]
|
||||
|
||||
md = /(bb)c(c(dd))(ee)/.match "XXaabbccddeeffXX"
|
||||
md.matchtree.assert == [["bb"], "c", ["c", ["dd"]], ["ee"]]
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
## Class#cattr
|
||||
|
||||
require 'facets/module/cattr'
|
||||
|
||||
class CAttrMockObject
|
||||
def initialize
|
||||
@@a = 10
|
||||
end
|
||||
def b ; @@b ; end
|
||||
end
|
||||
|
||||
Exception.refute.raised? do
|
||||
CAttrMockObject.class_eval{ cattr :a }
|
||||
end
|
||||
t = CAttrMockObject.new
|
||||
t.a.assert == 10
|
||||
|
||||
## Class#cattr_reader
|
||||
|
||||
Exception.refute.raised? do
|
||||
CAttrMockObject.class_eval { cattr_reader :a }
|
||||
end
|
||||
t = CAttrMockObject.new
|
||||
t.a.assert == 10
|
||||
|
||||
## Class#cattr_writer
|
||||
|
||||
Exception.refute.raised? do
|
||||
CAttrMockObject.class_eval { cattr_writer :b }
|
||||
end
|
||||
t = CAttrMockObject.new
|
||||
t.b = 5
|
||||
t.b.assert == 5
|
||||
|
||||
## Class#cattr_accessor
|
||||
|
||||
Exception.refute.raised? do
|
||||
CAttrMockObject.class_eval { cattr_accessor :c }
|
||||
end
|
||||
t = CAttrMockObject.new
|
||||
t.c = 50
|
||||
t.c.assert == 50
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
## Module#class_extend
|
||||
|
||||
require 'facets/module/class_extend'
|
||||
|
||||
Module using class_extend
|
||||
|
||||
mix = Module.new do
|
||||
def i ; end
|
||||
class_extend do
|
||||
def n ; 42 ; end
|
||||
def s ; self ; end
|
||||
end
|
||||
end
|
||||
|
||||
Extends module
|
||||
|
||||
mix.n.assert == 42
|
||||
mix.s.assert == mix
|
||||
|
||||
Has expected methods
|
||||
|
||||
mix.instance_methods.map{ |m| m.to_s }.assert == ['i']
|
||||
|
||||
transfers class methods to including class
|
||||
|
||||
mod = Module.new do
|
||||
include mix
|
||||
end
|
||||
mod.n.assert == 42
|
||||
mod.s.assert == mod
|
||||
|
||||
Doesn't overwrite instance methods
|
||||
|
||||
c = Class.new do
|
||||
include mix
|
||||
def n ; 11 ; end
|
||||
end
|
||||
c.new.n.assert == 11
|
||||
|
||||
Is overridable via including module
|
||||
|
||||
mod = Module.new do
|
||||
include mix
|
||||
class_extend do
|
||||
def n ; super + 1 ; end
|
||||
end
|
||||
end
|
||||
mod.n.assert == 43 # notice the difference!
|
||||
mod.s.assert == mod
|
||||
|
||||
Transfers class methods thru multiple levels of include
|
||||
|
||||
alt = Module.new do
|
||||
include mix
|
||||
class_extend do
|
||||
def n ; super + 1 ; end
|
||||
end
|
||||
end
|
||||
mod = Module.new do
|
||||
include alt
|
||||
end
|
||||
mod.n.assert == 43
|
||||
mod.s.assert == mod
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
## Module#class_inheritor
|
||||
|
||||
require 'facets/module/class_inheritor'
|
||||
|
||||
Subclass with inheritor
|
||||
|
||||
c = Class.new do
|
||||
class_inheritor :koko, [], :+
|
||||
koko! << 1
|
||||
end
|
||||
|
||||
d = Class.new(c) do
|
||||
class_inheritor :koko, [], :+
|
||||
koko! << 2
|
||||
end
|
||||
|
||||
We can see that
|
||||
|
||||
c.koko!.assert == [1]
|
||||
c.koko.assert == [1]
|
||||
|
||||
d.koko!.assert == [2]
|
||||
d.koko.assert == [1,2]
|
||||
|
||||
Subclass without class_inheritor
|
||||
|
||||
c = Class.new do
|
||||
class_inheritor :koko, [], :+
|
||||
koko! << 1
|
||||
end
|
||||
|
||||
d = Class.new(c)
|
||||
|
||||
Likewise
|
||||
|
||||
c.koko!.assert == [1]
|
||||
c.koko.assert == [1]
|
||||
|
||||
d.koko!.assert == []
|
||||
d.koko.assert == [1]
|
||||
|
||||
|
||||
Include module with class_inheritor
|
||||
|
||||
c = Class.new do
|
||||
class_inheritor :x, {}, :merge
|
||||
x![:a] = 1
|
||||
end
|
||||
|
||||
m = Module.new do
|
||||
class_inheritor :x, {}, :merge
|
||||
x![:b] = 2
|
||||
end
|
||||
|
||||
d = Class.new(c) do
|
||||
include m
|
||||
class_inheritor :x, {}, :merge
|
||||
x![:c] = 3
|
||||
end
|
||||
|
||||
Then
|
||||
|
||||
c.x.assert == {:a=>1}
|
||||
|
||||
m.x[:b].assert == 2
|
||||
|
||||
d.x.assert == {:a=>1,:b=>2,:c=>3}
|
||||
|
||||
c.x[:a].assert == 1
|
||||
c.x[:b].assert == nil
|
||||
c.x[:c].assert == nil
|
||||
|
||||
d.x[:a].assert == 1
|
||||
d.x[:b].assert == 2
|
||||
d.x[:c].assert == 3
|
||||
|
||||
And
|
||||
|
||||
d.x![:d] = 4
|
||||
d.x[:d].assert == 4
|
||||
|
||||
|
||||
Using #concat as the class_inheritor operator
|
||||
|
||||
c = Class.new do
|
||||
class_inheritor :relations, [], :concat
|
||||
end
|
||||
|
||||
d = Class.new(c) do
|
||||
#class_inheritor :relations, [], :concat
|
||||
end
|
||||
|
||||
c.relations! << 1
|
||||
c.relations! << 2
|
||||
d.relations! << 3
|
||||
|
||||
Notice
|
||||
|
||||
c.relations.assert == [1,2]
|
||||
d.relations.assert == [1,2,3]
|
||||
|
||||
|
||||
On Module
|
||||
|
||||
m = Module.new do
|
||||
class_inheritor :koko, [], :+
|
||||
koko! << 1
|
||||
end
|
||||
|
||||
c1 = Class.new do
|
||||
include m
|
||||
#class_inheritor :koko, [], :+
|
||||
koko! << 2
|
||||
koko! << 3
|
||||
end
|
||||
|
||||
c2 = Class.new do
|
||||
include m
|
||||
#class_inheritor :koko, [], :+
|
||||
koko! << 4
|
||||
end
|
||||
|
||||
m.koko.assert == [1]
|
||||
c1.koko.assert == [1,2,3]
|
||||
c2.koko.assert == [1,4]
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
## Module#copy_inheritor
|
||||
|
||||
require 'facets/module/copy_inheritor'
|
||||
|
||||
We see `#copy_inheritor` in action.
|
||||
|
||||
m = Module.new do
|
||||
copy_inheritor :koko, []
|
||||
koko << 1
|
||||
end
|
||||
|
||||
c1 = Class.new do
|
||||
include m
|
||||
#inheritor :koko, [], :+
|
||||
koko << 2
|
||||
koko << 3
|
||||
end
|
||||
|
||||
c2 = Class.new do
|
||||
include m
|
||||
#inheritor :koko, [], :+
|
||||
koko << 4
|
||||
end
|
||||
|
||||
And the result.
|
||||
|
||||
m.koko.assert == [1]
|
||||
c1.koko.assert == [1,2,3]
|
||||
c2.koko.assert == [1,4]
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
## Module#include_as
|
||||
|
||||
require 'facets/module/include_as'
|
||||
|
||||
Module#include_as utilizes #method_space to allow modules to be included
|
||||
within a separated namespace.
|
||||
|
||||
module T
|
||||
def t ; "Tt" ; end
|
||||
def r ; "Tr" ; end
|
||||
def q ; super ; end
|
||||
end
|
||||
|
||||
class N
|
||||
def q ; "Nq" ; end
|
||||
end
|
||||
|
||||
class X < N
|
||||
include_as :test => T
|
||||
|
||||
def n ; test.inspect ; end
|
||||
def m ; test.t ; end
|
||||
def o ; test.r ; end
|
||||
end
|
||||
|
||||
x = X.new
|
||||
|
||||
x.m.assert == "Tt"
|
||||
x.o.assert == "Tr"
|
||||
x.q.assert == "Nq"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
## Module#method_space
|
||||
|
||||
require 'facets/module/method_space'
|
||||
|
||||
class A
|
||||
attr_writer :x
|
||||
method_space :inside do
|
||||
def x; @x; end
|
||||
end
|
||||
end
|
||||
|
||||
a = A.new
|
||||
a.x = 10
|
||||
|
||||
We can see that the `inside` method space has access to the instance space.
|
||||
|
||||
a.inside.x #=> 10
|
||||
|
||||
But there is no reader in the instance space.
|
||||
|
||||
expect NoMethodError do
|
||||
a.x
|
||||
end
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user