• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

mrubyを超漢字で動作させる


Commit MetaInfo

Révision85b5af61544d730106271441c07246d9fa027daa (tree)
l'heure2015-11-15 05:15:37
AuteurYukihiro "Matz" Matsumoto <matz@ruby...>
CommiterYukihiro "Matz" Matsumoto

Message de Log

Merge branch 'nobu-feature/hash-cmp'

Change Summary

Modification

--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -250,4 +250,100 @@ class Hash
250250 def to_h
251251 self
252252 end
253+
254+ ##
255+ # call-seq:
256+ # hash < other -> true or false
257+ #
258+ # Returns <code>true</code> if <i>hash</i> is subset of
259+ # <i>other</i>.
260+ #
261+ # h1 = {a:1, b:2}
262+ # h2 = {a:1, b:2, c:3}
263+ # h1 < h2 #=> true
264+ # h2 < h1 #=> false
265+ # h1 < h1 #=> false
266+ #
267+ def <(hash)
268+ begin
269+ hash = hash.to_hash
270+ rescue NoMethodError
271+ raise TypeError, "can't convert #{hash.class} to Hash"
272+ end
273+ size < hash.size and all? {|key, val|
274+ hash.key?(key) and hash[key] == val
275+ }
276+ end
277+
278+ ##
279+ # call-seq:
280+ # hash <= other -> true or false
281+ #
282+ # Returns <code>true</code> if <i>hash</i> is subset of
283+ # <i>other</i> or equals to <i>other</i>.
284+ #
285+ # h1 = {a:1, b:2}
286+ # h2 = {a:1, b:2, c:3}
287+ # h1 <= h2 #=> true
288+ # h2 <= h1 #=> false
289+ # h1 <= h1 #=> true
290+ #
291+ def <=(hash)
292+ begin
293+ hash = hash.to_hash
294+ rescue NoMethodError
295+ raise TypeError, "can't convert #{hash.class} to Hash"
296+ end
297+ size <= hash.size and all? {|key, val|
298+ hash.key?(key) and hash[key] == val
299+ }
300+ end
301+
302+ ##
303+ # call-seq:
304+ # hash > other -> true or false
305+ #
306+ # Returns <code>true</code> if <i>other</i> is subset of
307+ # <i>hash</i>.
308+ #
309+ # h1 = {a:1, b:2}
310+ # h2 = {a:1, b:2, c:3}
311+ # h1 > h2 #=> false
312+ # h2 > h1 #=> true
313+ # h1 > h1 #=> false
314+ #
315+ def >(hash)
316+ begin
317+ hash = hash.to_hash
318+ rescue NoMethodError
319+ raise TypeError, "can't convert #{hash.class} to Hash"
320+ end
321+ size > hash.size and hash.all? {|key, val|
322+ key?(key) and self[key] == val
323+ }
324+ end
325+
326+ ##
327+ # call-seq:
328+ # hash >= other -> true or false
329+ #
330+ # Returns <code>true</code> if <i>other</i> is subset of
331+ # <i>hash</i> or equals to <i>hash</i>.
332+ #
333+ # h1 = {a:1, b:2}
334+ # h2 = {a:1, b:2, c:3}
335+ # h1 >= h2 #=> false
336+ # h2 >= h1 #=> true
337+ # h1 >= h1 #=> true
338+ #
339+ def >=(hash)
340+ begin
341+ hash = hash.to_hash
342+ rescue NoMethodError
343+ raise TypeError, "can't convert #{hash.class} to Hash"
344+ end
345+ size >= hash.size and hash.all? {|key, val|
346+ key?(key) and self[key] == val
347+ }
348+ end
253349 end
--- a/mrbgems/mruby-hash-ext/test/hash.rb
+++ b/mrbgems/mruby-hash-ext/test/hash.rb
@@ -158,3 +158,75 @@ assert("Hash#to_h") do
158158 assert_equal Hash, h.to_h.class
159159 assert_equal h, h.to_h
160160 end
161+
162+assert('Hash#<') do
163+ h1 = {a:1, b:2}
164+ h2 = {a:1, b:2, c:3}
165+
166+ assert_false(h1 < h1)
167+ assert_true(h1 < h2)
168+ assert_false(h2 < h1)
169+ assert_false(h2 < h2)
170+
171+ h1 = {a:1}
172+ h2 = {a:2}
173+
174+ assert_false(h1 < h1)
175+ assert_false(h1 < h2)
176+ assert_false(h2 < h1)
177+ assert_false(h2 < h2)
178+end
179+
180+assert('Hash#<=') do
181+ h1 = {a:1, b:2}
182+ h2 = {a:1, b:2, c:3}
183+
184+ assert_true(h1 <= h1)
185+ assert_true(h1 <= h2)
186+ assert_false(h2 <= h1)
187+ assert_true(h2 <= h2)
188+
189+ h1 = {a:1}
190+ h2 = {a:2}
191+
192+ assert_true(h1 <= h1)
193+ assert_false(h1 <= h2)
194+ assert_false(h2 <= h1)
195+ assert_true(h2 <= h2)
196+end
197+
198+assert('Hash#>=') do
199+ h1 = {a:1, b:2}
200+ h2 = {a:1, b:2, c:3}
201+
202+ assert_true(h1 >= h1)
203+ assert_false(h1 >= h2)
204+ assert_true(h2 >= h1)
205+ assert_true(h2 >= h2)
206+
207+ h1 = {a:1}
208+ h2 = {a:2}
209+
210+ assert_true(h1 >= h1)
211+ assert_false(h1 >= h2)
212+ assert_false(h2 >= h1)
213+ assert_true(h2 >= h2)
214+end
215+
216+assert('Hash#>') do
217+ h1 = {a:1, b:2}
218+ h2 = {a:1, b:2, c:3}
219+
220+ assert_false(h1 > h1)
221+ assert_false(h1 > h2)
222+ assert_true(h2 > h1)
223+ assert_false(h2 > h2)
224+
225+ h1 = {a:1}
226+ h2 = {a:2}
227+
228+ assert_false(h1 > h1)
229+ assert_false(h1 > h2)
230+ assert_false(h2 > h1)
231+ assert_false(h2 > h2)
232+end