Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
2fb12bd9
Commit
2fb12bd9
authored
Nov 26, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3034 from takahashim/try_convert
add {Array|Hash|String}.try_convert
parents
625a3fee
621487a0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
98 additions
and
0 deletions
+98
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+35
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+12
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+19
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+6
-0
mrbgems/mruby-string-ext/mrblib/string.rb
mrbgems/mruby-string-ext/mrblib/string.rb
+19
-0
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+7
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
2fb12bd9
class
Array
##
# call-seq:
# Array.try_convert(obj) -> array or nil
#
# Tries to convert +obj+ into an array, using +to_ary+ method.
# converted array or +nil+ if +obj+ cannot be converted for any reason.
# This method can be used to check if an argument is an array.
#
# Array.try_convert([1]) #=> [1]
# Array.try_convert("1") #=> nil
#
# if tmp = Array.try_convert(arg)
# # the argument is an array
# elsif tmp = String.try_convert(arg)
# # the argument is a string
# end
#
def
self
.
try_convert
(
obj
)
if
obj
.
respond_to?
(
:to_ary
)
obj
.
to_ary
else
nil
end
end
##
# call-seq:
# ary.uniq! -> ary or nil
...
...
@@ -709,4 +734,14 @@ class Array
end
nil
end
##
# call-seq:
# ary.to_ary -> ary
#
# Returns +self+.
#
def
to_ary
self
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
2fb12bd9
##
# Array(Ext) Test
assert
(
"Array.try_convert"
)
do
assert_nil
Array
.
try_convert
(
0
)
assert_nil
Array
.
try_convert
(
nil
)
assert_equal
[],
Array
.
try_convert
([])
assert_equal
[
1
,
2
,
3
],
Array
.
try_convert
([
1
,
2
,
3
])
end
assert
(
"Array#assoc"
)
do
s1
=
[
"colors"
,
"red"
,
"blue"
,
"green"
]
s2
=
[
"letters"
,
"a"
,
"b"
,
"c"
]
...
...
@@ -298,3 +305,8 @@ assert("Array#index (block)") do
assert_nil
(
1
..
10
).
to_a
.
index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
assert_equal
34
,
(
1
..
100
).
to_a
.
index
{
|
i
|
i
%
5
==
0
and
i
%
7
==
0
}
end
assert
(
"Array#to_ary"
)
do
assert_equal
[],
[].
to_ary
assert_equal
[
1
,
2
,
3
],
[
1
,
2
,
3
].
to_ary
end
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
2fb12bd9
...
...
@@ -60,6 +60,25 @@ class Hash
h
end
##
# call-seq:
# Hash.try_convert(obj) -> hash or nil
#
# Try to convert <i>obj</i> into a hash, using to_hash method.
# Returns converted hash or nil if <i>obj</i> cannot be converted
# for any reason.
#
# Hash.try_convert({1=>2}) # => {1=>2}
# Hash.try_convert("1=>2") # => nil
#
def
self
.
try_convert
(
obj
)
if
obj
.
respond_to?
(
:to_hash
)
obj
.
to_hash
else
nil
end
end
##
# call-seq:
# hsh.merge!(other_hash) -> hsh
...
...
mrbgems/mruby-hash-ext/test/hash.rb
View file @
2fb12bd9
...
...
@@ -39,6 +39,12 @@ assert('Hash.[] "c_key", "c_value"') do
end
end
assert
(
'Hash.try_convert'
)
do
assert_nil
Hash
.
try_convert
(
nil
)
assert_nil
Hash
.
try_convert
(
"{1=>2}"
)
assert_equal
({
1
=>
2
},
Hash
.
try_convert
({
1
=>
2
}))
end
assert
(
'Hash#merge!'
)
do
a
=
{
'abc_key'
=>
'abc_value'
,
'cba_key'
=>
'cba_value'
}
b
=
{
'cba_key'
=>
'XXX'
,
'xyz_key'
=>
'xyz_value'
}
...
...
mrbgems/mruby-string-ext/mrblib/string.rb
View file @
2fb12bd9
class
String
##
# call-seq:
# String.try_convert(obj) -> string or nil
#
# Try to convert <i>obj</i> into a String, using to_str method.
# Returns converted string or nil if <i>obj</i> cannot be converted
# for any reason.
#
# String.try_convert("str") #=> "str"
# String.try_convert(/re/) #=> nil
#
def
self
.
try_convert
(
obj
)
if
obj
.
respond_to?
(
:to_str
)
obj
.
to_str
else
nil
end
end
##
# call-seq:
# string.clear -> string
...
...
mrbgems/mruby-string-ext/test/string.rb
View file @
2fb12bd9
...
...
@@ -3,6 +3,13 @@
UTF8STRING
=
(
"
\343\201\202
"
.
size
==
1
)
assert
(
'String.try_convert'
)
do
assert_nil
String
.
try_convert
(
nil
)
assert_nil
String
.
try_convert
(
:foo
)
assert_equal
""
,
String
.
try_convert
(
""
)
assert_equal
"1,2,3"
,
String
.
try_convert
(
"1,2,3"
)
end
assert
(
'String#getbyte'
)
do
str1
=
"hello"
bytes1
=
[
104
,
101
,
108
,
108
,
111
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment