As it turns out, blocks are the ideal tool for use ion implementing user interfaces where we have a defined graphic that signifies a button and pressing that button would result in a pre-set series of processes. A simple button definition can be:
bPlay = Button.new("Play") bPause = Button.new("Pause") # so on and so forth......
If a user presses any of the buttons it calls on the pre-set reactions/processes that are supposed to happen if the user requests that action, say a method called "buttonSelected" will be called upon. The said method would then have to have it's own method as a sub-class of the button action sub-class. This can be done by defining the sub-class such as the sample code in the next post would show.
Sample Code:
class PlayButton < Button def initialize super("Play") # invoke Button's initialize process end def buttonSelected # do something that should be done when the start button is pressed end end
bPlay = PlayButton.new
This type of approach would bring about two very problematic scenarios; one, the actions that are to be done when a button is selected and pressed is not a function of the button itself but of the overall device which has the buttons as interface; and last, this would have us making tons of buttons that would correspond to all the function the overall device that if the said button's features or process were to change that would create a ton of maintenance issues in efforts to re-define those buttons to do all the different functions buttons should do in the whole application/device.
That's where blocks come in, to simplify the definition and use of these buttons making maintenance simpler so we could call the specific block to which we pass on the necessary parameters or methods to attain the final outcome. Below is an example of how this can be achieved with blocks creating a class called devicebutton:
class DeviceButton < Button def initialize(label, &process) super(label) @action = action end def buttonSelected @action.call(self) end end
bPlay = DeviceButton.new("Play") {cdList.play} bPause = DeviceButton.new("Pause") {cdList.pause}
We have looked into many of the data structures that are supported by ruby along with brief examples of their use and structures. Now we turn to the different data types the language supports which is vital in maximizing the potential of Ruby. Numbers are classified into integers and floating point numbers. The handling capabilities of Ruby when it comes to integers is dependent on the amount of memory you have installed on your computer. They are also classified into two object classes stored in either the Fixnum or Bignum classes. Ruby handles conversion to and from these two types automatically and stores them in binary form in their respective classes. Integers can also be used with optional lead signs that denote octal, hex, or binary.
Integer values can also be translated into their ASCII character set equivalents with the use of a ? before the variable. The term numeric literal that is denoted by an exponent or a decimal point is converted into a Float object in correspondence to the native language's double data type. To obtain the absolute value of an integer you use the form aNumber.abs and not abs(aNumber) as with C and C++.
About the Author
Jon Caldwell is a professional content manager. Much of his articles can be found at http://easyrubyonrailsprogramming.com
The Importance of Good Caravan Repairs
14 years ago
No comments:
Post a Comment