OSDN Git Service

Regular updates
[twpd/master.git] / ruby21.md
1 ---
2 title: Ruby 2.1
3 category: Ruby
4 layout: 2017/sheet
5 tags: [Archived]
6 intro: |
7   Quick reference to the [new features in Ruby 2.1](https://www.ruby-lang.org/).
8 ---
9
10 ### Named arguments with defaults
11
12 ```ruby
13 # length is required
14 def pad(num, length:, char: "0")
15   num.to_s.rjust(length, char)
16 end
17 ```
18
19 ```ruby
20 pad(42, length: 6) #=> "000042"
21 pad(42) #=> #<ArgumentError: missing keyword: length>
22 ```
23
24 ### Module.prepend
25
26 ```ruby
27 prepend(
28   Module.new do
29     define_method ...
30   end
31 )
32 ```
33
34 ### References
35
36 - http://globaldev.co.uk/2013/03/ruby-2-0-0-in-detail
37 - http://globaldev.co.uk/2014/05/ruby-2-1-in-detail