-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathruby.rb
32 lines (21 loc) · 1018 Bytes
/
ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Ruby Cheatsheet
## Date formatting
# NOTE: These are actually from ActiveSupport, not base Ruby.
# Show current named date formats.
pp Date::DATE_FORMATS
# Set custom date formats.
DateTime::DATE_FORMATS[:mine]="%Y-%m-%d %H:%M:%S"
Date::DATE_FORMATS[:month_and_year] = "%B %Y"
Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
# Use a date format.
date.to_fs(:short)
datetime.to_formatted_s(:mine)
# From https://github.com/caxlsx/caxlsx/blob/master/lib/axlsx/util/constants.rb
ISO_8601_REGEX = /\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/.freeze
## Files
file = File.open('path/to/file')
extension = File.extname(file).delete_prefix('.')
content_type ||= Mime::Type.lookup_by_extension(extension)
# Open Source TODO: Some File class methods should also be instance methods:
# - File.basename(file)
# - File.extname(file)