This repository was archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmisc.rb
96 lines (73 loc) · 2.18 KB
/
misc.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Miscellaneous extra methods and objects used by Autumn, and additions to Ruby
# Core objects.
# @private
class Numeric
# Possibly pluralizes a noun based on this number's value. Returns this number
# and the noun as a string. This method attempts to use the Ruby English gem
# if available, and falls back on the very simple default of appending an "s"
# to the word to make it plural. If the Ruby English gem is not available, you
# can specify a custom plural form for the word. Examples:
#
# 5.pluralize('dog') #=> "5 dogs"
# 1.pluralize('car') #=> "1 car"
# 7.pluralize('mouse', 'mice') #=> "7 mice" (only necessary if Ruby English is not installed)
def pluralize(singular, plural=nil)
begin
return "#{to_s} #{self == 1 ? singular : singular.plural}"
rescue Gem::LoadError
plural ||= singular + 's'
return "#{to_s} #{(self == 1) ? singular : plural}"
end
end
end
# @private
class String
# Returns a copy of this string with the first character dropped.
def except_first
self[1, size-1]
end
# Removes modules from a full class name.
def demodulize
split("::").last
end
end
# @private
class Hash
# Returns a hash that gives back the key if it has no value for that key.
def self.parroting(hsh={})
hsh ||= Hash.new
Hash.new { |h, k| k }.update(hsh)
end
end
# @private
#
# An implementation of `SizedQueue` that, instead of blocking when the queue is
# full, simply discards the overflow, forgetting it.
class ForgetfulQueue < Queue
# Creates a new sized queue.
def initialize(capacity)
super()
@max = capacity
@mutex = Mutex.new
end
# Returns true if this queue is at maximum size.
def full?
size == @max
end
# Pushes an object onto the queue. If there is no space left on the queue,
# does nothing.
def push(obj)
super unless full?
end
alias_method :<<, :push
alias_method :enq, :push
end
# @private Adds the only method to Set.
class Set
# Returns the only element of a one-element set. Raises an exception if there
# isn't exactly one element in the set.
def only
raise IndexError, "Set#only called on non-single-element set" unless size == 1
to_a.first
end
end