Browse by Category
Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:
- Write the most Transcendental, Imbroglio Ruby...
READ
Have you ever been confused by the __underscores__ required by some of __RUBY__'s features? You can get it right with this overview of all of "super snake" keywords and methods.
READ
The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language.
Even though the Unicode standard evolves constantly - it gets updated at least once a year - Ruby's Unicode support is often only a little bit behind the current version of Unicode. The following tables list which Ruby version supports which version of Unicode / Emoji:
READ
Recent Ruby versions allow you to choose from a wide-range of uppercase letters - beyond just ASCII - to start a constant / class name:
READ
Ruby's mode of operation can be altered with some --enable-*
/ --disable-*
command-line switches.
READ
What is your wild guess: How many different ways does Ruby provide for inserting a NULL byte into a double-quoted string?
READ
How does nothing (as in nil, null, or nan) compare to nothing?
READ
The introduction of pattern matching in Ruby 2.7 brought us a new style of multi-assigning local variables: The pattern assignment, or, how you could also call it, the assignment in-style.
READ
Ruby's Warning module learned some new tricks in Ruby 2.7:
READ
Ruby comes with good support for Unicode-related features. Read on if you want to learn more about important Unicode fundamentals and how to use them in Ruby…
…or just watch my talk from RubyConf 2017:
⑩ Unicode Characters You Should Know About as a 👩💻
READ
Starting with Ruby 2.5¹ it is possible to customize the behavior of Kernel#warn through the Warning module. Here is how:
READ
Regexes, the go-to-mechanism for string matching, must not only be written, but also need to be applied. This episode acts as a reference with some style advice for working with regular expressions in Ruby. If you are looking for resources on writing the actual regexes, take a look at the link collection at the bottom.
READ
When you get farther upwards the steep hill that is Ruby mastery, you will come across some powerful, yet slightly evil methods: instance_eval and class_eval¹. They allow you to execute code and define methods tied to a specific class, at the same time giving you access to outer scope variables through the Ruby block syntax. Their exact behavior varies, depending on the context they are used in. So what is the difference between all the evals?
¹ Also aliased as module_eval
READ
Ruby was created in 1993 and has come a long way. The preferred style of coding has changed quite a lot and solid best practice has emerged (though, as always, one size does not fit all). At the same time, Ruby's tool support could be better, the language is still too complex. Maybe, the time has come to remove some features from Ruby.
READ
Double-quoted strings can not only be used with interpolation, #{}
, they also support various escape sequences, which are initiated with \
. Escape sequences allow you to embed raw byte and codepoint values. Furthermore, there are shortcuts for common formatting and control characters.
READ
Ruby has more than one way to access additional information about the most recent regex match, like captured groups. One way is using the special variables $`
, $&
, $'
, $1
- $9
, $+
, and also in the MatchData object $~
. They become available after using a method that matched a regex, or when the method supports a block, they are already available in the block.
READ
Ruby's big DATA
constant does more than you might expect!
READ
Ruby supports magic comments (interpreter instructions) at the top of the source file, mostly known for setting a source files' Encoding. This is the most common use case, but there is more you can do.
READ
%a
%A
%b
%B
%c
%C
%d
%D
%e
%F
%g
%G
%h
%H
%I
%j
%k
%l
%L
%m
%M
%n
%N
%p
%P
%Q
%r
%R
%s
%S
%t
%T
%u
%U
%v
%V
%w
%W
%x
%X
%y
%Y
%z
%Z
%+
%%
-
_
0
^
#
:
Date and time formatting is traditionally done with strftime. Not any different in Ruby, which includes a public domain based strftime implementation accessible via Time#strftime. Ruby would not be Ruby if it would not add some confusion: There is a second implementation included in the standard library which is used by Date#strftime and DateTime#strftime. It behaves similarly in most cases, but also differs in some nuances (for example, additional formatting directives like %Q
are supported).
READ
How come that Ruby has two ASCII encodings?
READ
Another of Ruby's idiosyncrasies is equalness. It's not too complicated, but naming is an issue here.
READ
Similar to metaprogramming, Ruby's type conversion system has evolved over time. While the result functions, it is also a little inconsistent and suffers from poor naming. Let's put things in perspective:
READ
Generated with Ruby 3.1.0, including RubyGems and DidYouMean:
Object (Class)
├─ARGF (ARGF.class)
├─ARGV (Array)
├─ArgumentError (Class)
├─Array (Class)
├─BasicObject (Class)
│ └─BasicObject → BasicObject
├─Bignum (Class)
│ └─GMP_VERSION (String)
...
READ
It is less common, but similar to methods, constants have a visibility attached to them. You have the choice between private and public, and you can also mark a constant deprecated!
READ
How many bytes (= ASCII characters) of Ruby code does it take to generate a SHA 256 hash sum of STDIN?
READ
Some words should not be chosen as identifiers for variables, parameters and methods in Ruby, because they clash with core methods or keywords.
READ
%a
%A
%b
%B
%c
%d
%e
%E
%f
%g
%G
%i
%o
%p
%s
%u
%x
%X
%%
0
$
#
+
-
*
space
Ruby comes with a structured alternative to classic string interpolation: This episode will explore format strings, also known as the sprintf syntax.
READ
Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:
- Write the most Transcendental, Imbroglio Ruby...
READ
Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:
- Write the most Transcendental, Imbroglio Ruby...
READ
There is nothing easier than parsing the command-line arguments given to your Ruby program: It is an array found in the special variable $*
:
READ
Today, another snippet from the category don't try at home, might have unforeseeable consequences!
READ
In case you have wondered, what this top-level constant TOPLEVEL_BINDING
is all about:
READ
What happens when you invoke the Ruby interpreter, even before it executes your first line of code? Actually a lot! A few observations:
READ
At some point when working with Ruby, you come across this mysterious RbConfig constant. A typical scenario is that you want to check which operating system your current program is executed on. You can do this with RbConfig::CONFIG['host_os']
or RbConfig::CONFIG['arch']
, see the RubyGems source for an advanced example!
READ
Ruby's Regexp engine has a powerful feature built in: It can match for Unicode character properties. But what exactly are properties you can match for?
READ
When exactly don't you have to :"escape"
a Ruby symbol?
READ
A quick reminder that number literals in Ruby can be pretty fancy!
READ
In general, Ruby's reflection capabilities are pretty powerful, although not always logical. However, when reflecting on a method's (or proc's) usage, you are sometimes stuck with sad methods.
READ
Programming languages have been, and will always be categorized by their typing system. Naturally, large parts of the Ruby community (including myself) have some kind of aversion against static typing. But while Ruby goes down the route of being dynamically typed that does not mean that you are not allowed to use some form of types!
2020 update: Ruby 3.0 introduced types
READ
ERB stands for <%# Embedded Ruby %> and is the templating engine included in the Ruby Standard Library. While there are more recent gems that provide a better templating experience (see tilt for an abstraction, and erubis/erbse for an updated ERB), it is also convenient to have basic support directly in the standard library.
READ
Is it a hash? Or is it a hash?
READ
RubyGems is bundled with core Ruby since 1.9, which was first released in 2007. As long as you do not run Ruby with the $ ruby --disable-gems
flag, it is available to you without having to install anything. This also means that you can use some of RubyGems' support utilities for free!
READ
Ruby's syntax is so expressive — it utilizes every printable, non-alphanumeric ASCII character as much as it cans. Sometimes, this can be confusing for beginners.
READ
If you don't like errors in your code, you will have to fix them. This handy list of Ruby's errors will hopefully help you do so! (And welcome back for the second season of Idiosyncratic Ruby!)
READ
This is a summary of reasons why you should still use Ruby:
It is a language with a terrific community (welcoming and always questioning itself) and ecosystem (a lot of problems already solved), which is beautiful (focus on productivity), a little conservative (in the sense that it is easy to work with), and general purpose (a good choice in most cases).
For a deeper understanding of Ruby's principles, reading matz is highly recommended:
READ
Ruby's regex engine defines a lot of shortcut character classes. Besides the common meta characters (\w
, etc.), there is also the POSIX style expressions and the unicode property syntax. This is an overview of all character classes:
READ
If you take a closer look, you'll notice that Ruby's grammar has quite a few edge-case where its syntax is inconsistent or ambiguous:
READ
There is an operator in Ruby, that does nothing: The unary plus operator. It is part of the language for keeping symmetry with the unary minus operator!
READ
Code Golf is the art of writing the shortest program possible. The less bytes the better. And the competition is just ridiculously strong! Head over to Anarchy Golf if you want to see more!
READ
Ruby has three default encodings. One of them is the default source encoding, which can be set using a magic comment in the file's first line, or in the second line if the first line is taken by a shebang.
READ
Ruby clutters its objects a lot with methods for metaprogramming other methods:
READ
If you change one line in Ruby's source, it will support goto statements!
READ
Tired of good old, but slow Ruby? Try one of Ruby's successors:
READ
Ruby has a built-in feature that is much like Literate CoffeeScript. In contrast to it, this Ruby option will not ignore literature, but garbage:
READ
Ruby's URI standard library contains a very sophisticated regex for matching URLs:
READ
A guide to Ruby's stdlib and available alternatives.
Please note: The standard library was turned into gems, see stdgems.org for up-to-date information on all standard gems!
Standard Library Overview
READ
This episode takes a look at the unusual use of symbols in one of Ruby's core APIs:
READ
Ruby's Struct class is a convenient way to create Ruby classes, which already have some attributes defined. If you are not familiar with structs, you should watch Avdi Grimm's introduction to structs!
But in many cases there is something better than structs:
READ
One of Ruby's goals was to replace popular unix stream editors like awk
or sed
, which both have the concept of manipulating files in a line-based manner. Ruby has the -n
option for this:
READ
Ruby allows you to change key functionality of the language. This means, it is also possible to break key functionality! Six examples of Ruby code you should never use:
READ
All Ruby syntaxes¹ that represent the R string literal:
READ
There are two very different ways to create local variables in Ruby. You are probably familiar with the classical way:
READ
Ruby puts a lot of effort into its Enumerable module, offering a lot of different ways of iterating through collections. It is one of the reasons for Ruby's success, but you can also call it idiosyncratic, sometimes. This episode portraits enumerables' three handy slice_*
methods.
READ
Some of IRB's command-line options can be called idiosyncratic as well. Take math mode¹ as an example: It will require the infamous mathn library on start up:
READ
You are here for a collection of 10 advanced features of regular expressions in Ruby!
READ
Ruby's ENV object gives you access to environment variables. It looks like normal Ruby hash:
READ
This is an overview of all the special, two-letter (and other) global variables in Ruby, which Ruby inherited from Perl.
For the purpose of improving code readability, Ruby comes with English.rb in its standard library (or Deutsch.rb as gem), which provides non-cryptic aliases and some documentation.
Ruby also defines some three-letter global variables that mirror CLI options ($-…
)
READ
One of the never-ending style battles in Ruby land is module_function
vs extend self
.
READ
There is a command-line switch to enable command-line switches:
READ
There is a one-liner on the internet that will start a local web server, for serving all the static files in your current directory:
READ
The script lines feature is probably the most famous example for idiosyncratic naming in Ruby!
READ
a
A
b
B
c
C
d
D
e
E
F
g
G
h
H
l
L
m
m0
M
n
N
p
P
q
Q
s
S
u
U
v
V
w
x
X
Z
@
!
*
<
>
_
Ruby comes equipped with a powerful option for low level string manipulation: String#unpack
and its counter part Array#pack
. Today's episode takes a closer look.
READ
Ruby has some ways to turn on debug mode, which library authors can use to print out extra information for interested users. But unfortunately, there are multiple debug modes in Ruby. When to use which one?
READ
Ruby strings are powerful. You could say Ruby is built around manipulating strings. There are tons of ways how to work with strings and as of Ruby 2.7.1 String
offers 130 instance methods. Knowing them well can save you a lot of time.
READ
Compared to other languages, Ruby does not have very good tool support for development. This might not be a problem for many of us: In the end, humans create the code, and not the tools.
Nevertheless, it would be great to have better tools. Or at least valid syntax highlighting!
READ