Introducing the Rails core_ext series
I think code reading matters. And I’m not the only one who thinks so. Recently, I’ve been thinking about giving myself a goal regarding code reading. And I was thinking to set this goal to something related to Rails because I really want to dig into Rails code. So, while reading the appendixes of the rails three way, I realized that the Rails extensions to Ruby core are cool and, above all, there are many things I didn’t know. So wrapping up these thoughts I’ve set the following goal: I’ll try to cover all the Rails core_ext with some posts. I’ll try to understand how to use the stuff and how it actually works. I hope that writing some posts will help me to complete this goal and to do it quickly. Furthermore, this series could be useful mainly for the beginners that want to dig into Rails. Code reading matters, you know.
Ruby is a such a powerful language that allows the programmer to add/modify the behaviour of any class, even the classes that comes with the language itself. Many of us know that this is a very cool feature if you use in a proper way. And this is the main reason that convinced Rails core_ext. Rails is a large, wide-used and well-written Ruby project. Its source code is absolutely worth reading.
Rails source is organized in sub-project and the project that contains the core extensions is activesupport. In the subdirectory lib/active_support/core_ext you’ll find the following directories (I know… it’s a quite long list):
|~array/
| |-access.rb
| |-conversions.rb
| |-extract_options.rb
| |-grouping.rb
| |-random_access.rb
| |-uniq_by.rb
| `-wrap.rb
|~big_decimal/
| `-conversions.rb
|~class/
| |-attribute.rb
| |-attribute_accessors.rb
| |-delegating_attributes.rb
| |-inheritable_attributes.rb
| `-subclasses.rb
|~date/
| |-acts_like.rb
| |-calculations.rb
| |-conversions.rb
| |-freeze.rb
| `-zones.rb
|~date_time/
| |-acts_like.rb
| |-calculations.rb
| |-conversions.rb
| `-zones.rb
|~file/
| |-atomic.rb
| `-path.rb
|~float/
| `-rounding.rb
|~hash/
| |-conversions.rb
| |-deep_dup.rb
| |-deep_merge.rb
| |-diff.rb
| |-except.rb
| |-indifferent_access.rb
| |-keys.rb
| |-reverse_merge.rb
| `-slice.rb
|~integer/
| |-inflections.rb
| |-multiple.rb
| `-time.rb
|~kernel/
| |-agnostics.rb
| |-debugger.rb
| |-reporting.rb
| |-requires.rb
| `-singleton_class.rb
|~module/
| |-aliasing.rb
| |-anonymous.rb
| |-attr_accessor_with_default.rb
| |-attr_internal.rb
| |-attribute_accessors.rb
| |-delegation.rb
| |-deprecation.rb
| |-introspection.rb
| |-method_names.rb
| |-reachable.rb
| |-remove_method.rb
| `-synchronization.rb
|~numeric/
| |-bytes.rb
| `-time.rb
|~object/
| |-acts_like.rb
| |-blank.rb
| |-conversions.rb
| |-duplicable.rb
| |-inclusion.rb
| |-instance_variables.rb
| |-to_json.rb
| |-to_param.rb
| |-to_query.rb
| |-try.rb
| `-with_options.rb
|~process/
| `-daemon.rb
|~range/
| |-blockless_step.rb
| |-conversions.rb
| |-cover.rb
| |-include_range.rb
| `-overlaps.rb
|~string/
| |-access.rb
| |-behavior.rb
| |-conversions.rb
| |-encoding.rb
| |-exclude.rb
| |-filters.rb
| |-inflections.rb
| |-inquiry.rb
| |-interpolation.rb
| |-multibyte.rb
| |-output_safety.rb
| |-starts_ends_with.rb
| |-strip.rb
| `-xchar.rb
|~time/
| |-acts_like.rb
| |-calculations.rb
| |-conversions.rb
| |-marshal.rb
| |-publicize_conversion_methods.rb
| `-zones.rb
|-array.rb
|-benchmark.rb
|-big_decimal.rb
|-class.rb
|-enumerable.rb
|-exception.rb
|-file.rb
|-float.rb
|-hash.rb
|-integer.rb
|-kernel.rb
|-load_error.rb
|-logger.rb
|-module.rb
|-name_error.rb
|-numeric.rb
|-object.rb
|-proc.rb
|-process.rb
|-range.rb
|-regexp.rb
|-rexml.rb
|-string.rb
|-uri.rb
In order to maintain some sanity is a good idea to organize code putting all the stuff related to a topic in a directory and putting a bunch of require statements in a ruby file with the same name of the topic directory. So, for example, if you want to take a look at all the code written by the awesome Rails guys related to the Hash class you’re likely to find the hash.rb. It contains a bunch of require statements. In this way you could get an idea of the stuff related to hash at a glance.
For a couple of days, I’ve been thinking how to organize the single posts of this series. I like to extract some patterns for a series because if I have a pattern to follow I could stay focused of the topic and write better. I have already done it with my reviews and I am satisfied. You have to consider that organizing stuff in English for a non-native speaker isn’t a simple task, at least for me. By the way, I will use the following pattern:
- Show the code
- Describe its intents
- Describe how it works
- Search for use in rails code
- Try some example in irb
I wrote the pattern down in the introduction post because I’d like to get some suggestion about it, I’m quite excited about this series. Furthermore, This post will be the table of content of the series.