2014-05-07 08:12:54 +00:00
|
|
|
---
|
|
|
|
title: Ruby 2.1
|
2015-11-24 05:02:17 +00:00
|
|
|
category: Ruby
|
2020-07-03 12:47:47 +00:00
|
|
|
tags: [Archived]
|
|
|
|
intro: |
|
|
|
|
Quick reference to the [new features in Ruby 2.1](https://www.ruby-lang.org/).
|
2014-05-07 08:12:54 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
### Named arguments with defaults
|
|
|
|
|
2020-07-03 12:47:47 +00:00
|
|
|
```ruby
|
|
|
|
# length is required
|
|
|
|
def pad(num, length:, char: "0")
|
|
|
|
num.to_s.rjust(length, char)
|
|
|
|
end
|
|
|
|
```
|
2014-05-07 08:12:54 +00:00
|
|
|
|
2020-07-03 12:47:47 +00:00
|
|
|
```ruby
|
|
|
|
pad(42, length: 6) #=> "000042"
|
|
|
|
pad(42) #=> #<ArgumentError: missing keyword: length>
|
|
|
|
```
|
2014-05-07 08:12:54 +00:00
|
|
|
|
|
|
|
### Module.prepend
|
|
|
|
|
2020-07-03 12:47:47 +00:00
|
|
|
```ruby
|
|
|
|
prepend(
|
|
|
|
Module.new do
|
|
|
|
define_method ...
|
|
|
|
end
|
|
|
|
)
|
|
|
|
```
|
2014-05-07 08:12:54 +00:00
|
|
|
|
|
|
|
### References
|
|
|
|
|
2020-07-03 12:47:47 +00:00
|
|
|
- http://globaldev.co.uk/2013/03/ruby-2-0-0-in-detail
|
|
|
|
- http://globaldev.co.uk/2014/05/ruby-2-1-in-detail
|