<commit_before># :computer: Idiomatic-ruby

A collection of Ruby idioms and patterns.

## Table of Contents

1.  [Curly brackets and map](#curly-brackets-and-map)
2.  [Keyword arguments](#keyword-arguments)
3.  [Singleton class](#singleton-class)
4.  [Local variables on the go](#local-variables-on-the-go)
5.  [Exception raising](#exception-raising)
6.  [Literal constructors](#literal-constructors)
7.  [Here doc](#here-doc)
8.  [Syntactic sugar](#syntactic-sugar)
9.  [Set](#set)
10. [Hash](#hash)
11. [Enumerators vs Iterators](#enumerators-vs-iterators)
12. [Regexp](#regexp)
13. [Method chaining](#method-chaining)
14. [Range to array](#range-to-array)
15. [Block arguments](#block-arguments)
16. [dup vs clone](#dup-vs-clone)
17. [Struct inheritance](https://github.com/apostergiou/idiomatic-ruby/blob/master/struct_inheritance.rb)
18. [Binary ambersand](#binary-ambersand)
19. [Inline rescue](#inline-rescue)
20. [alias vs alias_method](#alias-vs-alias_method)
21. [block vs hash](#block-vs-hash)
22. [Null object pattern](#null-object-pattern)
23. [Constant scope](#constant-scope)
24. [Block variables](#block-variables)
25. [Method missing](#method-missing)
26. [Equality](#equality)
27. [Printing](#printing)
28. [Class variable access](#class-variable-access)
29. [tap](#tap)
30. [Environment variables](#environment-variables)
31. [refine](#refine)
32. [proc method](#proc-method)

## Curly brackets and map

```ruby
cake_prices = [2, 3, 5]

puts cake_prices.map { |p| p * 3.14 }    # => prints the cake prices
puts cake_prices.map do |p| p * 3.14 end # => puts(cake_prices.map) do |n| n * 10 end
```

**[⬆ back to top](#table-of-contents)**

## Keyword arguments

Ruby 2.1 introduced required keyword arguments. If a required argument is missing(the arguments defined with a trailing colon are required), Ruby will tell us by raising ArgumentError.

```ruby
def foo(cake, bar:)
  puts bar
end

foo                   # => ArgumentError: missing keyword: bar
foo(true)             # => ArgumentError: missing keyword: bar
foo(true, 'baz')      # => ArgumentError: missing keyword: bar
foo(true, bar: 'baz') # => 'baz'
```

**[⬆ back to top](#table-of-contents)**

## Singleton class
<commit_msg>Use a better format for the table of contents<commit_after># :computer: Idiomatic-ruby

A collection of Ruby idioms and patterns.

## Table of Contents

- [Curly brackets and map](#curly-brackets-and-map)
- [Keyword arguments](#keyword-arguments)
- [Singleton class](#singleton-class)
- [Local variables on the go](#local-variables-on-the-go)
- [Exception raising](#exception-raising)
- [Literal constructors](#literal-constructors)
- [Here doc](#here-doc)
- [Syntactic sugar](#syntactic-sugar)
- [Set](#set)
- [Hash](#hash)
- [Enumerators vs Iterators](#enumerators-vs-iterators)
- [Regexp](#regexp)
- [Method chaining](#method-chaining)
- [Range to array](#range-to-array)
- [Block arguments](#block-arguments)
- [dup vs clone](#dup-vs-clone)
- [Struct inheritance](https://github.com/apostergiou/idiomatic-ruby/blob/master/struct_inheritance.rb)
- [Binary ambersand](#binary-ambersand)
- [Inline rescue](#inline-rescue)
- [alias vs alias_method](#alias-vs-alias_method)
- [block vs hash](#block-vs-hash)
- [Null object pattern](#null-object-pattern)
- [Constant scope](#constant-scope)
- [Block variables](#block-variables)
- [Method missing](#method-missing)
- [Equality](#equality)
- [Printing](#printing)
- [Class variable access](#class-variable-access)
- [tap](#tap)
- [Environment variables](#environment-variables)
- [refine](#refine)
- [proc method](#proc-method)

## Curly brackets and map

```ruby
cake_prices = [2, 3, 5]

puts cake_prices.map { |p| p * 3.14 }    # => prints the cake prices
puts cake_prices.map do |p| p * 3.14 end # => puts(cake_prices.map) do |n| n * 10 end
```

**[⬆ back to top](#table-of-contents)**

## Keyword arguments

Ruby 2.1 introduced required keyword arguments. If a required argument is missing(the arguments defined with a trailing colon are required), Ruby will tell us by raising ArgumentError.

```ruby
def foo(cake, bar:)
  puts bar
end

foo                   # => ArgumentError: missing keyword: bar
foo(true)             # => ArgumentError: missing keyword: bar
foo(true, 'baz')      # => ArgumentError: missing keyword: bar
foo(true, bar: 'baz') # => 'baz'
```

**[⬆ back to top](#table-of-contents)**

## Singleton class

{'fraction_non_alphanumeric': 0.16514598540145986, 'fraction_numerical': 0.018476277372262775, 'mean_word_length': 4.636246786632391, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '30939', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1429, 'n_words': 398}