Procs and lambdas
-
Ok I'm touching a little on the subject of Procs and lambda, they are quite new to me.
I guess if I started programming outside Sketchup I would have stumbled upon this earlier..Tried to gather some information about the subject and tested a little.
But since Sketchup enviroment is a little special, I wonder if there are some best practices
to follow or even if one should avoid using them.I havent found any answers for:
Can they get overused? Should Procs only be used with short codeblocks?
So in practice I was iterating a Hash and wanted to reuse this iteration in the same method
for another Hash.
1st impulse was to create another method and call it from inside this method, but I notice the argumentlist became quite long and started to wonder if this will have major impact on the performance since there might be a lot of elements iterated.
Tried using Proc.new inside the method, and yeay it worked. But everything good has a downside, right?So just keep sticking to def happyMethod end, or have I entered the dark side of programming and found a jewel?
Edit: Hm guess not. (probably not using the Proc in a correct way)
Proc.new is actually slightly slower than using a simple method call, when testing
with some denser geometry(in my case).
So I can only guess it costs more to create the Proc object then sending variables to an outside method? And maybe Procs are not meant to be used like a "function"? -
If you are concerned about performance: test, test and re-test. Only thing that will give you correct answers. There are so many things where I'd never have guessed what would have been the fastest way around it.
In general, creating objects in ruby is expensive. Reuse as much as you can.
-
Yeah, that is probably what I have to do.
In this case I backed of a little and removed some features, and speed is more resonable.
I guess one can't be to greedy in Ruby. -
What does "reuse the iteration" mean?
-
@unknownuser said:
What does "reuse the iteration" mean?
Hm. maybe my explanations and english where bad. Hash.each{|k, v| v.do something } is called an "iteration", right?
Iterate a Hash. Depending on something do the same iteration on other Hash with different values.
Seams like waste retyping the same code again..I know you are asking why I don't pass in a nested Hash, or array and just do everything in 1 go. In this case I just cant.
Advertisement