-3

As the title states, what is this output? I tried checking if it's an array or a hash with nextPart.to_a(Hash)? and nextPart.kind_of?(Array), using both Hash and Array and result is always false.

So what is this output and how do I get and change variables like @rotated?

#<JoMojo_cutz::Cutz::SheetPart:0x0000000ed2c658 @rotated=false, @append=false>

I've tried... and a few other variations with no luck...

nextPart.rotated
nextPart[rotated]
nextPart[@rotated]
JoMojo
  • 404
  • 1
  • 7
  • 22
  • 3
    It is a `JoMojo_cutz::Cutz::SheetPart` instance (ie. an object that is not an Array or Hash). [@variables are instance variables](http://www.rubyist.net/~slagell/ruby/instancevars.html). While one *can* use [`#instance_variable_set`](http://ruby-doc.org/core-2.3.3/Object.html#method-i-instance_variable_set), it would be better to *Read the documentation* to find out how the API is designed to work. – user2864740 Dec 20 '16 at 06:14
  • I count 21 instance variables. Is it not possible to give an example with fewer, perhaps `@rotated` and one other? – Cary Swoveland Dec 20 '16 at 06:25
  • @CarySwoveland sorry for the length of the example, I just cut and paste the output without realizing it's length. Edited the example. Noticed you're a woodworker, so am I, and I'm learning ruby so I can try and modify a sketchup cut sheet plugin. – JoMojo Dec 20 '16 at 13:20

1 Answers1

3

You can always write nextPart.instance_variable_get("@rotated") or nextPart.instance_variable_set("@rotated", "new value")

But it's better to add attr_accessor :rotated to the class - then you can do nextPart.rotated and nextPart.rotated = "new value".

For each of the instance variables you want to define these dot-methods for, you can add it to the attr_accessor:

attr_accessor :rotated, :thickness
# etc.

See What is attr_accessor in Ruby?

Also, keep in mind there are many classes in Ruby. You can call nextPart.class to see what it is. In this case, you can tell from this that it's a JoMojo_cutz::Cutz::SheetPart:

#<JoMojo_cutz::Cutz::SheetPart:0x0000000ed2c658

This is kind of a Rubyism that you can just come to recognize. When you see

#<ClassName:ObjectId

you can just know that it's an instance.

Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Appreciate the explanation, right after I posted my question a found that exact question about attr_accessor. I've gotten lucky so far learning ruby by modifying and studying other's code but this particular code is a mess and a real pain to try and figure out! – JoMojo Dec 20 '16 at 21:50