# How to do this?

**URL:** <https://www.ruby-forum.com/t/how-to-do-this/145762>\
**Category:** Ruby\
**Created:** [August 13, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/how-to-do-this/145762 "2008-08-13T19:40:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [August 13, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/how-to-do-this/145762/1 "2008-08-13T19:40:35Z")

</div>

Dear sirs,

I am planning a simple geometry generation library. XYZ points,  
primitive  
geometry such as triangles and guides that can generate more geometry.

I feel somehow retarted, as usual, so I hope to hear some wise words of  
advice.

So, suppose I would want to use the following syntax to command the  
little XYZ -points into geometric arrangements of beauty and elegance,  
especially the ‘along’ -bit, where starcircle is defined:

#generates a star by combining two triangles  
astar = Guide.polygon((Symbol.triangle(99) + Symbol.triangle(99).rotate  
(180))

#generates a circle with stars replacing the points on the circumference  
n\_int = 20  
radius\_int = 500  
starcircle = Symbol.with\_polygon(astar.polygon).along(Symbol.circle  
(n\_int, radius\_int)))  
Pic = starcircle.render(“svg”)  
#etc …

1. How would one manage the generation of starcircle? Store the star  
(“astar”) passed as argument in with\_polygon in a Guide-instance and  
refer to it once “along”-method is called.

Am I doing this wrong? Is this just artistic nonsens? Should it read, in  
a boring CS major style:  
starcircle = Symbol.generate\_with\_guide(astar, Symbol.circle)

1. How to achieve the ‘+’ operation for symbols? For example symbol4 +  
somesymbol would join the geometry of the latter into the former. Or  
perhaps return a new symbol with combined geometry. How is that handled  
for adding number-objects?

I am hoping to achieve as flexible and robust syntax as possible, suited  
for adventurous artist types who might try things your average engineer  
would think were “wrong”.

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [August 13, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/how-to-do-this/145762/2 "2008-08-13T20:25:43Z")

</div>

> 1. How to achieve the ‘+’ operation for symbols? For example symbol4 +  
> somesymbol would join the geometry of the latter into the former. Or  
> perhaps return a new symbol with combined geometry. How is that handled  
> for adding number-objects?

This doesn’t completely answer your question, but you can define a +  
(value) method on any class. You can write any logic you want in it.

E.g.

class TestPlus  
def initialize(value=0)  
@value = value  
end

def +(value)  
# replace this with any logic you want. +value+ can be any sort of  
object, not just a number  
@value += value  
puts @value  
end  
end

x = TestPlus.new(0)  
x + 2  
=\> 2

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [August 13, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/how-to-do-this/145762/3 "2008-08-13T20:38:01Z")

</div>

On 8/13/08, Casimir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dear sirs,
> 
> I am planning a simple geometry generation library. XYZ points, primitive  
> geometry such as triangles and guides that can generate more geometry.

I’ll offer my opinions in reverse order:

> 1. How to achieve the ‘+’ operation for symbols? For example symbol4 +  
> somesymbol would join the geometry of the latter into the former. Or  
> perhaps return a new symbol with combined geometry. How is that handled  
> for adding number-objects?

Most (maybe even all) built-in + operators return a new object, so I’d  
do the same for your Symbols. Assuming a Symbol is just a collection  
of primitives (lines and points), then it should probably be something  
like:

class Symbol  
def initialize prims=[]  
@primitives = prims  
end  
def + other  
Symbol.new(@primitives +other.primitives )  
end  
end

> So, suppose I would want to use the following syntax …
> 
> starcircle = Symbol.with\_polygon(astar.polygon).along(Symbol.circle  
> (n\_int, radius\_int)))
> 
> 1. How would one manage the generation of starcircle? Store the star  
> (“astar”) passed as argument in with\_polygon in a Guide-instance and  
> refer to it once “along”-method is called.

That could work, but is awkward. What happens if you call #along  
without #with first? What if you call #with twice?

> Am I doing this wrong? Is this just artistic nonsens? Should it read, in  
> a boring CS major style:  
> starcircle = Symbol.generate\_with\_guide(astar, Symbol.circle)

How about a more ruby style:  
tri =Symbol.triangle(side)  
astar = tri + tri.rotate(180)  
circle = Symbol.circle(pts,radius)  
starcircle = circle.map\_points{|point| astar.at(point).primitives }

where Symbol#at creates a new instance of itself centered on point, and  
Symbol#map\_points creates a new symbol based on the block results:

def map\_points &block  
new\_primitives = []  
@points.each{|pt| new\_primitives\<\< block.call(pt)}  
Symbol.new(newpoints)  
end

-Adam

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [August 14, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/how-to-do-this/145762/4 "2008-08-14T15:45:56Z")

</div>

Adam S. kirjoitti:

> On 8/13/08, Casimir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Dear sirs,
> > 
> > I am planning a simple geometry generation library. XYZ points, primitive  
> > geometry such as triangles and guides that can generate more geometry.
> 
> I’ll offer my opinions in reverse order:

Thank you Daniel C. and Adam S… I will now build the simple  
geometry classes and return here on this topic with some code once I  
have implemented the Symbol and Guide classes.

Casimir P.
