/
kernel18.rb
173 lines (144 loc) · 3.67 KB
/
kernel18.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- encoding: us-ascii -*-
module Kernel
def method(name)
name = Rubinius::Type.coerce_to_symbol name
cm = Rubinius.find_method(self, name)
if cm
Method.new(self, cm[1], cm[0], name)
else
raise NameError, "undefined method `#{name}' for #{self.inspect}"
end
end
alias_method :send, :__send__
alias_method :object_id, :__id__
def to_a
if self.kind_of? Array
self
else
[self]
end
end
def loop
raise LocalJumpError, "no block given" unless block_given?
begin
while true
yield
end
rescue StopIteration
end
end
module_function :loop
def rand(limit=0)
unless limit == 0
limit = Integer(limit).abs
end
case limit
when 0
Thread.current.randomizer.random_float
when Integer
Thread.current.randomizer.random_integer(limit - 1)
else
raise TypeError, "Integer() returned a non-integer"
end
end
module_function :rand
def Integer(obj)
case obj
when Integer
obj
when Float
if obj.nan? or obj.infinite?
raise FloatDomainError, "unable to coerce #{obj} to Integer"
else
obj.to_int
end
when String
if obj.empty?
raise ArgumentError, "invalid value for Integer: (empty string)"
else
obj.to_inum(0, true)
end
else
# Can't use coerce_to or try_convert because I think there is an
# MRI bug here where it will return the value without checking
# the return type.
if obj.respond_to? :to_int
if val = obj.to_int
return val
end
end
Rubinius::Type.coerce_to obj, Integer, :to_i
end
end
module_function :Integer
def open(path, *rest, &block)
path = StringValue(path)
if path.kind_of? String and path.prefix? '|'
return IO.popen(path[1..-1], *rest, &block)
end
File.open(path, *rest, &block)
end
module_function :open
alias_method :proc, :lambda
module_function :proc
def String(obj)
return obj if obj.kind_of? String
unless obj.respond_to? :to_s
raise TypeError, "Unable to convert to a String"
end
str = obj.to_s
unless str.kind_of? String
raise TypeError, "#to_s did not return a String"
end
return str
end
module_function :String
def Array(obj)
ary = Rubinius::Type.check_convert_type obj, Array, :to_ary
return ary if ary
if obj.respond_to? :to_a
Rubinius::Type.coerce_to(obj, Array, :to_a)
else
[obj]
end
end
module_function :Array
def Float(obj)
raise TypeError, "can't convert nil into Float" if obj.nil?
case obj
when Float
obj
when String
valid_re = /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
m = valid_re.match(obj)
if !m or !m.pre_match.empty? or !m.post_match.empty?
raise ArgumentError, "invalid value for Float(): #{obj.inspect}"
end
obj.convert_float
else
coerced_value = Rubinius::Type.coerce_to(obj, Float, :to_f)
if coerced_value.nan?
raise ArgumentError, "invalid value for Float(): #{coerced_value.inspect}"
end
coerced_value
end
end
module_function :Float
def id
Kernel.warn "Object#id IS deprecated; use Object#object_id OR ELSE."
__id__
end
def type
Kernel.warn "Object#type IS fully deprecated; use Object#class OR ELSE."
self.class
end
def method(name)
name = Rubinius::Type.coerce_to_symbol name
cm = Rubinius.find_method(self, name)
if cm
return Method.new(self, cm[1], cm[0], name)
else
raise NameError, "undefined method `#{name}' for #{self.inspect}"
end
end
end