SketchUp Arrays & Enumerable
-
If I type the following in the Ruby Console:
Array.ancestors [Array, Enumerable, Object, Kernel]
it shows Enumerable is mixed in Array. Is there some reason I can use some methods from Enumerable such as each_with_index, but not each_slice?
-
you need to require 'enumerator' to be able to use each_slice
-
@unknownuser said:
you need to require 'enumerator' to be able to use each_slice
Why does each_with_index work, while each_slice does not? Both are from Enumerable.
It seems like only parts of Enumerable are included. Maybe the version of Ruby compiled with SketchUp did not have each_slice at the time.
-
if you check the enum.c (Ruby C code for Enumerable class) you will see that only each_with_index is there and each_slice
is part of ext/enumerator.c -
So here's a list of all the instance methods of an Array in a SketchUp/Ruby array. It has some elements of a regular Ruby Array, some methods of Enumerable, and of course the SketchUp specific methods.
["&", "*", "+", "-", "<<", "[]", "[]=", "assoc", "at", "clear", "collect", "collect!", "compact", "compact!", "concat", "cross", "delete", "delete_at", "delete_if", "distance", "distance_to_line", "distance_to_plane", "dot", "each", "each_index", "empty?", "fetch", "fill", "first", "flatten", "flatten!", "index", "indexes", "indices", "insert", "join", "last", "length", "map", "map!", "nitems", "normalize", "normalize!", "offset", "offset!", "on_line?", "on_plane?", "pack", "pop", "project_to_line", "project_to_plane", "push", "rassoc", "reject", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "select", "shift", "size", "slice", "slice!", "sort", "sort!", "to_ary", "transform", "transform!", "transpose", "uniq", "uniq!", "unshift", "values_at", "vector_to", "x", "x=", "y", "y=", "z", "z=", "zip", "|"]
I don't think it's complete because I still I missed "each_with_index"
-
Maybe it depends on the Sketchup version you are using? Mine is 6.4.112 and the result of "puts Array.new.methods.sort" is:
& * + - << <=> == === =~ [] []= __id__ __send__ all? any? assoc at class clear clone collect collect! compact compact! concat cross delete delete_at delete_if detect display distance distance_to_line distance_to_plane dot dup each each_index each_with_index empty? entries eql? equal? extend fetch fill find find_all first flatten flatten! freeze frozen? get_binding grep hash id include? index indexes indices inject insert inspect instance_eval instance_of? instance_variable_get instance_variable_set instance_variables is_a? join kind_of? last length map map! max member? method methods min nil? nitems normalize normalize! object_id offset offset! on_line? on_plane? pack partition pop private_methods project_to_line project_to_plane protected_methods public_methods push rassoc reject reject! replace respond_to? reverse reverse! reverse_each rindex select send shift singleton_methods size slice slice! sort sort! sort_by taint tainted? to_a to_ary to_b to_s transform transform! transpose type uniq uniq! unshift untaint values_at vector_to x x= y y= z z= zip |
-
Ah, I used Array.instance_methods but I see that it requires an agument of "true" to get the inherited and mix-in methods also. ( 98 methods )
(Array.instance_methods(true) - Object.methods).sort ["&", "*", "+", "-", "<<", "[]", "[]=", "all?", "any?", "assoc", "at", "clear", "collect", "collect!", "compact", "compact!", "concat", "cross", "delete", "delete_at", "delete_if", "detect", "distance", "distance_to_line", "distance_to_plane", "dot", "each", "each_index", "each_with_index", "empty?", "entries", "fetch", "fill", "find", "find_all", "first", "flatten", "flatten!", "grep", "index", "indexes", "indices", "inject", "insert", "join", "last", "length", "map", "map!", "max", "member?", "min", "nitems", "normalize", "normalize!", "offset", "offset!", "on_line?", "on_plane?", "pack", "partition", "pop", "project_to_line", "project_to_plane", "push", "rassoc", "reject", "reject!", "replace", "reverse", "reverse!", "reverse_each", "rindex", "select", "shift", "size", "slice", "slice!", "sort", "sort!", "sort_by", "to_ary", "transform", "transform!", "transpose", "uniq", "uniq!", "unshift", "values_at", "vector_to", "x", "x=", "y", "y=", "z", "z=", "zip", "|"]
Advertisement