-
Notifications
You must be signed in to change notification settings - Fork 2
/
core_util.rb
60 lines (44 loc) · 1006 Bytes
/
core_util.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Musical stuff
Semitone = 2.0 ** (1.0 / 12.0)
def one_divided_by(n)
1.0 / n.to_f
end
def gen_id
SecureRandom.urlsafe_base64
end
# File patch
class File
def self.most_recently_edited(glob)
Dir.glob(glob).max_by { |f| File.mtime(f) }
end
end
# String patch
class String
def is_number?
self =~ /^[0-9\.]+$/
end
def file_identifier
# i.e. gets 'kick' from '/my/path/kick.mp3'
File.basename self, ".*"
end
end
# Integer patch
class Integer
# integer in this context means number of steps up in a musical scale
# see MusicalScale instance methods which return a delta percentage
# depending on which scale is used.
def to_musical_scale_step
Sounds::MusicalScaleStep.new(self)
end
end
# Thread patch
Thread.abort_on_exception = true
# multithreading can be turned off, for debugging
# the reason for doing this would be to set breakpoints using byebug
if ENV["SINGLE_THREADED"]
class Thread
def self.new(&blk)
blk.call
end
end
end