try_convert

Array.try_convert(obj) â array or nil
Class Public methods

Tries to convert obj into an array, using to_ary method. Returns the 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.

1
2
3
4
5
6
7
8
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
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.