SketchUp Arrays & Enumerable

SketchUp Arrays & Enumerable

Postby Jim » Sat Aug 09, 2008 3:46 pm

If I type the following in the Ruby Console:

Code: Select all
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?
0

Jim 
Global Moderator
 

Re: SketchUp Arrays & Enumerable

Postby tbd » Sat Aug 09, 2008 6:12 pm

you need to require 'enumerator' to be able to use each_slice
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: SketchUp Arrays & Enumerable

Postby Jim » Sat Aug 09, 2008 6:26 pm

TBD wrote: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.
0

Jim 
Global Moderator
 

Re: SketchUp Arrays & Enumerable

Postby tbd » Sat Aug 09, 2008 6:40 pm

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 ;)
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: SketchUp Arrays & Enumerable

Postby Jim » Thu Aug 28, 2008 11:14 am

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.

Code: Select all
["&", "*", "+", "-", "<<", "[]", "[]=", "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"
0

Jim 
Global Moderator
 

Re: SketchUp Arrays & Enumerable

Postby azuby » Thu Aug 28, 2008 11:32 am

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:
Code: Select all
&
*
+
-
<<
<=>
==
===
=~
[]
[]=
__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
|
0
error initus :: Blocks | CurrentDate | d/Code | extensionmanager | FFlipper | HideEdges | MeasuredArea | ModelHistory* | PluginsHelp | PronButton | SAWSO | SCP | SU²CATT

Bad English? PM me, correct me. :)

azuby 
 

Re: SketchUp Arrays & Enumerable

Postby Jim » Fri Aug 29, 2008 5:42 pm

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 )

Code: Select all
(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", "|"]

0

Jim 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 3 guests

Visit our sponsors: