If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
[Ruby] Color (revisited)
4 posts
• Page 1 of 1
[Ruby] Color (revisited)
It is such a small thing that I don't think it belongs to "user examples".
Two years or so ago, I talked about some hidden features of some classes. The Color class provides .getARGB and .setARGB. With those you can manipulate an existing Color instance instead of creating a new class instance. Doing so will unburden the GC, especially in situations, where a constant high redraw rate is involved.
But it was not until yesterday, that I discovered the support for the splat operator! While getARGB returns an array, setARGB expects single integer values (up to 4). With the splat operator, you can provide an array directly. For example
Also, there's ._dump. It seems to be used internally for marshalling, so I wouldn't recommend using it. However, it returns a string
Ok, that's it
Two years or so ago, I talked about some hidden features of some classes. The Color class provides .getARGB and .setARGB. With those you can manipulate an existing Color instance instead of creating a new class instance. Doing so will unburden the GC, especially in situations, where a constant high redraw rate is involved.
But it was not until yesterday, that I discovered the support for the splat operator! While getARGB returns an array, setARGB expects single integer values (up to 4). With the splat operator, you can provide an array directly. For example
- Code: Select all
a = Color.new(221)
b = Color.new(32, 64, 128)
a.setARGB(*b.getARGB)
Also, there's ._dump. It seems to be used internally for marshalling, so I wouldn't recommend using it. However, it returns a string
- Code: Select all
# continued code section
a._dump(nil) # returns "255:32:64:128"
Ok, that's it
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: [Ruby] Color (revisited)
Yes, the splat operator is extremely useful. Where I personally found it most useful is with __send__ method. The method receives a label of a method as first argument, followed by arguments for that method. It allows you to avoid long case statements. Where I utilized it the most as in Real Time Strategy game (that I never actually finished).
The action is decided when player interacts with the GUI,
but the action need to be executed in game loop (calling the tick method). Normally, the tick method would need to have large case statement to account for every action the unit can do (which can be different for different units), like this:
The splat operator with __send__ method completely avoid that issue, make the code more streamline, flexible and easier to debug.
- Code: Select all
class Unit
#create unit at coordinates x,y
def initialize(x,y,health,damage)
@x,@y=x,y
@health=health
@damage=damage
@action=nil
end
def tick()
self.__send__(*@action) if @action
end
def move_to(x,y)
@x,@y=x,y
end
def attack(target)
target.health-=@damage
end
end
The action is decided when player interacts with the GUI,
- Code: Select all
def mouseLDown(x,y)
@selected_unit.action=[:move_to,x,y]
end
but the action need to be executed in game loop (calling the tick method). Normally, the tick method would need to have large case statement to account for every action the unit can do (which can be different for different units), like this:
- Code: Select all
def tick()
case @action[0]
when :attack
attack(@action[1])
when :move_to
move_to(@action[2],@action[3])
end
end
The splat operator with __send__ method completely avoid that issue, make the code more streamline, flexible and easier to debug.
- KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
Re: [Ruby] Color (revisited)
A very good example!
Also, Ruby's handling of the splat operator is far more intelligent than people might think. For example, test this code and see what it prints on the pane.
Very handy!
And for Flowstone 4, Maik has updated Ruby to 2.5, which not only means a ton of improvements (for example a complete GC overhaul), but also the doublesplat operator. Similar to the splat operator, but for keyword arguments and therefore also for hashes. Combined you can do things like mymethod(a, *b, **c) to seperate keyword arguments from all other arguments.
Also, Ruby's handling of the splat operator is far more intelligent than people might think. For example, test this code and see what it prints on the pane.
- Code: Select all
def test(a, b, *c, d)
watch [a, b, c, d]
end
f = [1, 2, 3]
test(0, *f, 4)
Very handy!
And for Flowstone 4, Maik has updated Ruby to 2.5, which not only means a ton of improvements (for example a complete GC overhaul), but also the doublesplat operator. Similar to the splat operator, but for keyword arguments and therefore also for hashes. Combined you can do things like mymethod(a, *b, **c) to seperate keyword arguments from all other arguments.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: [Ruby] Color (revisited)
tulamide wrote:And for Flowstone 4, Maik has updated Ruby to 2.5, which not only means a ton of improvements (for example a complete GC overhaul), but also the doublesplat operator. Similar to the splat operator, but for keyword arguments and therefore also for hashes. Combined you can do things like mymethod(a, *b, **c) to seperate keyword arguments from all other arguments.
I've read about the doublesplat. Quite useful... In fact, I've run into several situations where a method has multiple optional arguments and currently there is no simple way to enter specific ones or omit specific others - the order in which arguments are passed to optional arguments has a fixed rule.
Btw, explanation to those who don't understand Tula's test code above:
1. test(0, *f, 4) ...the splat unfolds the "f" array, so effectively the line passes this: test(0, 1, 2, 3, 4)
2. def test(a, b, *c, d) ...first the obligatory arguments in the beginning are filled (a=0; b=1). Then obligatory arguments on the end get filled from the end down (d=4). All remaining middle arguments (if any) are passed to the "c" as array.
- KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
4 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 63 guests