|
1 | | -# Ruby Facets |
| 1 | +# Ruby Facets <img src="docs/assets/images/cherries.svg" alt="cherries" width="34" height="34"> |
2 | 2 |
|
3 | 3 | [](https://rubygems.org/gems/facets) |
4 | 4 | [](https://github.com/rubyworks/facets/actions/workflows/ci.yml) |
5 | 5 |
|
| 6 | +**More of Ruby, one method at a time.** Facets is a collection of extensions to Ruby's core classes and standard library, plus a few small, reusable classes and modules. Most methods live in their own files, so you can load one extension, a class's extensions, or the core collection. |
6 | 7 |
|
7 | | -*"ALL YOUR BASE ARE BELONG TO RUBY"* |
| 8 | +Facets began in 2005 and is still maintained. The current release is **3.2.2**, which requires **Ruby 3.1 or newer**. See the [release history](HISTORY.md) for changes and migration notes from earlier versions. The `main` branch also contains changes awaiting the next release. |
8 | 9 |
|
| 10 | +## Install |
9 | 11 |
|
10 | | -## Introduction |
| 12 | +```sh |
| 13 | +gem install facets |
| 14 | +``` |
11 | 15 |
|
12 | | -Ruby Facets is the premier collection of general purpose method |
13 | | -extensions and standard additions for the Ruby programming language. |
| 16 | +With Bundler, add this to your Gemfile: |
14 | 17 |
|
15 | | -Facets houses the largest single collection of methods available for |
16 | | -extending the core capabilities of Ruby's built-in classes and modules. |
17 | | -This collection of extension methods are unique by virtue of their atomicity. |
18 | | -The methods are stored in individual files so that each can be required |
19 | | -independently. This gives developers the potential for much finer control over |
20 | | -which extra methods to bring into their code. |
| 18 | +```ruby |
| 19 | +gem 'facets', require: false |
| 20 | +``` |
21 | 21 |
|
22 | | -In addition Facets provides a collection of extensions to Ruby standard library |
23 | | -plus a small collection of add-on classes and modules. Together these |
24 | | -libraries constitute an reliable source of reusable components, suitable |
25 | | -to a wide variety of usecases. |
| 22 | +`require: false` lets you choose which extensions to load. Omit it if you want Bundler to load the core collection automatically. |
26 | 23 |
|
| 24 | +## Choose how much to load |
27 | 25 |
|
28 | | -## Resources |
| 26 | +### One method |
29 | 27 |
|
30 | | -* Homepage: https://rubyworks.github.io/facets |
31 | | -* Report Bugs: https://github.com/rubyworks/facets/issues |
32 | | -* Wiki Pages: https://github.com/rubyworks/facets/wiki |
33 | | -* Source Code: https://github.com/rubyworks/facets |
| 28 | +```ruby |
| 29 | +require 'facets/array/to_ranges' |
34 | 30 |
|
| 31 | +[1, 2, 3, 6, 7].to_ranges |
| 32 | +#=> [1..3, 6..7] |
| 33 | +``` |
35 | 34 |
|
36 | | -## Documentation |
37 | | - |
38 | | -Facets has special documentation needs due to its extensive breadth. |
39 | | -The documentation generated when installing via RubyGems, or the YARD |
40 | | -docs provided by rubydoc.info can be somewhat unwieldy because it |
41 | | -combines all of Facets in one large set. When using these resources, |
42 | | -it is important to remain aware of the source location of particular |
43 | | -methods. |
44 | | - |
45 | | -For better organized online documentation, generated to separate core |
46 | | -extensions from standard libraries, see the [Learn Facets](https://rubyworks.github.io/facets/learn.html) page on the website for links to available documentation. |
47 | | - |
48 | | - |
49 | | -## Installation |
50 | | - |
51 | | -### Bundler |
52 | | - |
53 | | -If you are using Bundler with your project, add the facets gem to the project's |
54 | | -Gemfile. Unless you want all of facets loaded be sure to add the `:require => false` |
55 | | -option. |
56 | | - |
57 | | - gem "facets", require: false |
58 | | - |
59 | | -### RubyGems |
60 | | - |
61 | | -The easiest way to install is via RubyGems. |
62 | | - |
63 | | - $ gem install facets |
64 | | - |
65 | | -### Requirements |
66 | | - |
67 | | -Facets 3.2+ requires Ruby 3.1 or higher. |
68 | | - |
69 | | - |
70 | | -## Mission |
71 | | - |
72 | | -Facets holds to the notion that the more we can *reasonably* integrate into |
73 | | -a common foundation, directed toward general needs, the better that foundation |
74 | | -will be able to serve the community. There are a number of advantages here: |
75 | | - |
76 | | -* Better Code-reuse |
77 | | -* Collaborative Improvements |
78 | | -* Greater Name Consistency |
79 | | -* One-stop Shop and Installation |
80 | | - |
81 | | - |
82 | | -## Usage |
83 | | - |
84 | | -### CORE Library |
85 | | - |
86 | | -At the heart of Ruby Facets is the CORE extensions library. CORE provides |
87 | | -a sizable collection of generally useful methods, along with a few supporting |
88 | | -classes, that extend the functionality of Ruby's core classes and modules. |
89 | | - |
90 | | -With the exception of a few *uncommon* extensions, CORE contains anything that |
91 | | -will load automatically when issuing: |
92 | | - |
93 | | - require 'facets' |
94 | | - |
95 | | -This loads all the CORE functionality at once. If you plan to use more then a |
96 | | -handful of Facets core methods it is recommended that you require the library in |
97 | | -this way. However, you can also "cherry pick" the CORE library as you prefer. |
98 | | -And for uncommon extensions this must be done. The general require statement for |
99 | | -a core extension library is: |
100 | | - |
101 | | - require 'facets/<class|module>/<method>' |
102 | | - |
103 | | -For example: |
104 | | - |
105 | | - require 'facets/time/stamp' |
106 | | - |
107 | | -Most "atoms" contain only one method, but exceptions occur when methods |
108 | | -are closely tied together. |
| 35 | +### One class's core extensions |
109 | 36 |
|
110 | | -You can load per-class or per-module groups of core methods by requiring the |
111 | | -class or module by name. For example" |
| 37 | +```ruby |
| 38 | +require 'facets/string' |
112 | 39 |
|
113 | | - require 'facets/time' |
| 40 | +'Ruby Facets'.snakecase |
| 41 | +#=> "ruby_facets" |
| 42 | +``` |
114 | 43 |
|
115 | | -Will require all the core Time method extensions. |
| 44 | +### The core collection |
116 | 45 |
|
117 | | -Note that some methods that were part of CORE in 1.8 and earlier are now part |
118 | | -of MORE libraries. A good example is 'random.rb'. There were separated because |
119 | | -they had more specialized use cases, where as CORE extensions are intended as |
120 | | -general purpose. |
| 46 | +```ruby |
| 47 | +require 'facets' |
121 | 48 |
|
122 | | -#### Method File Names |
| 49 | +[1, 2, 3].average |
| 50 | +#=> 2.0 |
| 51 | +``` |
123 | 52 |
|
124 | | -Operator method redirect files are stored using English names. For instance |
125 | | -`Proc#*` is `proc/op_mul`. |
| 53 | +`require 'facets'` loads the broadly useful **core** extensions. Some specialized core extensions are opt-in; require their method file directly. To load Facets extensions to a Ruby standard library, require that library through Facets: |
126 | 54 |
|
127 | | -For reference, here is the chart. |
| 55 | +```ruby |
| 56 | +require 'facets/ostruct' |
| 57 | +``` |
128 | 58 |
|
129 | | - +@ => op_plus |
130 | | - -@ => op_minus |
131 | | - + => op_add |
132 | | - - => op_sub |
133 | | - ** => op_pow |
134 | | - * => op_mul |
135 | | - / => op_div |
136 | | - % => op_mod |
137 | | - ~ => op_tilde |
138 | | - <=> => op_cmp |
139 | | - << => op_lshift |
140 | | - >> => op_rshift |
141 | | - < => op_lt |
142 | | - > => op_gt |
143 | | - === => op_case |
144 | | - == => op_equal |
145 | | - =~ => op_apply |
146 | | - <= => op_lt_eq |
147 | | - >= => op_gt_eq |
148 | | - | => op_or |
149 | | - & => op_and |
150 | | - ^ => op_xor |
151 | | - []= => op_store |
152 | | - [] => op_fetch |
| 59 | +This loads `ostruct` and Facets' OpenStruct extensions. On Ruby 3.5+, declare the `ostruct` gem separately because it is no longer a default gem. |
153 | 60 |
|
154 | | -Facets simply takes the '*' and translates it into a string acceptable to all |
155 | | -file systems. Also, if a method ends in '=', '?' or '!' it is simply removed. |
156 | | - |
157 | | - |
158 | | -### MORE Library (aka Standard Library) |
159 | | - |
160 | | -On top of the extensive CORE library, Facets provides extensions for Ruby's |
161 | | -standard library, as well as a small collection of additional modules and |
162 | | -classes to supplement it. |
163 | | - |
164 | | -Use this library like you would any other 3rd party library. |
165 | | -The only difference between Facet's Standard library and other libraries |
166 | | -is the lack of any enclosing `Facets::` namespace. |
167 | | - |
168 | | -When using Facets extended versions of Ruby's standard libraries, |
169 | | -the libraries have to loaded individually. However you do not need |
170 | | -to load Ruby's library first, as the Facets' library will do that |
171 | | -automatically. |
172 | | - |
173 | | -For example, normally one load Ruby's OpenStruct class via: |
174 | | - |
175 | | - require 'ostruct' |
176 | | - |
177 | | -To load 'ostruct.rb' plus Facets extensions for it simply use: |
178 | | - |
179 | | - require 'facets/ostruct' |
| 61 | +## Documentation |
180 | 62 |
|
181 | | -For details pertaining to the functionality of each feature, |
182 | | -please see the API documentation. |
| 63 | +- [Getting started and loading guide](https://rubyworks.github.io/facets/learn.html) |
| 64 | +- [Generated API documentation on RubyDoc.info](https://www.rubydoc.info/gems/facets) (check the displayed version) |
| 65 | +- [Release history](HISTORY.md) |
183 | 66 |
|
| 67 | +In the published 3.2.2 gem, the split between `lib/core` and `lib/standard` is visible in API source paths. This helps you tell whether a method is loaded by `require 'facets'` or needs an explicit require. The development branch also has a new `lib/rails` area for Rails-compatible helpers; see the **Unreleased** section of the release history for details. |
184 | 68 |
|
185 | 69 | ## Contribute |
186 | 70 |
|
187 | | -This project thrives on contribution! |
188 | | - |
189 | | -If you have any extension methods, classes or modules that you think have |
190 | | -very general applicability and would like to see them included in |
191 | | -this project, don't hesitate to submit. Also, if you have better versions |
192 | | -of any thing already included or simply have a patch, they are more than |
193 | | -welcome. We want Ruby Facets to be of the highest quality. |
194 | | - |
195 | | - |
196 | | -## Development |
197 | | - |
198 | | -Facets uses the [Lemon](https://rubyworks.github.io/lemon) testing framework |
199 | | -to handle unit testing, while [QED](https://rubyworks.github.io/qed) specifications |
200 | | -provide tested documentation. Run the test suite with [Rake](https://ruby.github.io/rake/): |
201 | | - |
202 | | - $ rake test |
203 | | - |
204 | | -Continuous integration runs on GitHub Actions (see `.github/workflows/ci.yml`). |
205 | | - |
206 | | - |
207 | | -## Authors |
208 | | - |
209 | | -Much of this collection was written and/or inspired by a variety of great Ruby |
210 | | -developers. Fortunately nearly all utilized works were copyrighted under the same |
211 | | -open licenses, the Ruby License or the more liberal BSD and MIT licenses. In the |
212 | | -one or two exceptions the copyright notice has been included with the source code. |
213 | | -We have since received permission from the various authors to normalize the licensing |
214 | | -to a single license. For this purpose we have chosen the BSD 2 Clause License. |
215 | | -This is the license Ruby itself now uses, so it seemed the most appropriate choice. |
216 | | -It is also almost identical to the MIT license. Any code file not specifically labeled |
217 | | -otherwise shall fall under the this license (which is BSD 2-clause). |
218 | | - |
219 | | -In all cases, every effort has been made to give credit where credit is due. |
220 | | -You will find these acknowledgments embedded in the source code. You can see |
221 | | -them in "CREDIT:" and/or "@author" lines. |
222 | | -Also see the [Contributors page](https://github.com/rubyworks/facets/wiki/Contributors) |
223 | | -on the Wiki for a list of all contributing Rubyists. If anyone is missing from |
224 | | -the list, please let us know so we can correct. Thanks. |
225 | | - |
226 | | -This collection was put together by, and much of it written by [trans](https://github.com/trans). |
227 | | -If need be, he can be reached via email at transfire at gmail.com. |
228 | | - |
229 | | - |
230 | | -## License |
231 | | - |
232 | | -The collection PER COLLECTION is licensed as follows: |
233 | | - |
234 | | - Ruby Facets |
235 | | - Copyright (c) 2005 Rubyworks |
236 | | - |
237 | | - Distributed under the terms of the BSD-2 License (same as Ruby license). |
238 | | - |
239 | | -The BSD 2 Clause License is a simple open source license. The complete text of the |
240 | | -license accompany this document (see the enclosed LICENSE file). |
241 | | - |
242 | | -Acknowledgments and Copyrights for particular snippets of borrowed code |
243 | | -are given in their respective source. At this point, all licensing has been normalized |
244 | | -for all included code. Original authors have given permission for inclusion of their |
245 | | -code under such license, with appropriate credit citations. |
| 71 | +Issues and pull requests are welcome. Start with [CONTRIBUTING.md](CONTRIBUTING.md) for the library's method organization, demos, and test conventions. The test suite runs with: |
246 | 72 |
|
| 73 | +```sh |
| 74 | +bundle install |
| 75 | +bundle exec rake test |
| 76 | +``` |
247 | 77 |
|
248 | | -## "ALL YOUR BASE ARE BELONG TO RUBY!" |
| 78 | +[Source](https://github.com/rubyworks/facets) · [Issues](https://github.com/rubyworks/facets/issues) · [Website](https://rubyworks.github.io/facets/) |
249 | 79 |
|
250 | | -Ruby Facets, Copyright (c) 2005 Rubyworks |
| 80 | +## License and credits |
251 | 81 |
|
252 | | -Do you Ruby? (https://ruby-lang.org) |
| 82 | +Facets is distributed under the [BSD 2-Clause License](LICENSE.txt). Thomas Sawyer started the project, and many Rubyists have contributed code, ideas, tests, and documentation. Individual files record additional credits where applicable. |
253 | 83 |
|
| 84 | +*All your base are belong to Ruby.* |
0 commit comments