Friday, March 20, 2020
Global Variables in Ruby
Global Variables in Ruby Global Variables are variables that may be accessed from anywhere in the program regardless of scope. Theyre denoted by beginning with a $ (dollar sign) character. However, the use of global variables is often considered un-Ruby, and you will rarely see them. Defining Global Variables Global variables are defined and used like any other variable. To define them, simply assign a value to them and begin using them. But, as their name suggests, assigning to global variables from any point in the program has global implications. The following program demonstrates this. The method will modify a global variable, and that will affect how the second method runs. $speed 10 def accelerate $speed 100 end def pass_speed_trap if $speed 65 # Give the program a speeding ticket end end accelerate pass_speed_trap Unpopular So why is this un-Ruby and why dont you see global variables very often? Put simply, it breaks encapsulation. If any one class or method can modify the state of the global variables at will with no interface layer, any other classes or methods that rely on that global variable may behave in an unexpected and undesirable manner. Further, such interactions can be very difficult to debug. What modified that global variable and when? Youll be looking through quite a lot of code to find what did it, and that could have been avoided by not breaking the rules of encapsulation. But thats not to say that global variables are never used in Ruby. There are a number of special global variables with single-character names (a-la Perl) that can be used throughout your program. They represent the state of the program itself, and do things like modify the record and field separators for all gets methods. Global Variables $0 - This variable, denoted by $0 (thats a zero), holds the name of the top-level script being executed. In other words, the script file that was run from the command line, not the script file that holds the currently executing code. So, if script1.rb was run from the command line, it would hold script1.rb. If this script requires script2.rb, $0 in that script file would also be script1.rb. The name $0 mirrors the naming convention used in UNIX shell scripting for the same purpose.$* - The command-line arguments in an array denoted by $* (dollar sign and asterisk). For example, if you were to run ./script.rb arg1 arg2, then $* would be equivalent to %w{ arg1 arg2 }. This is equivalent to the special ARGV array and has a less descriptive name, so it is rarely used.$$ - The interpreters process ID, denoted by $$ (two dollar signs). Knowing ones own process ID is often useful in daemon programs (which run in the background, unattached from any terminal) or system services. However, this gets a bit more complicated when threads are involved, so be wary of using it blindly. $/ and $\ - These are the input and output record separators. When you read objects using gets and print them using puts, it uses these to know when a complete record has been read, or what to print between multiple records. By default, these should be the newline character. But since these affect the behavior of all IO objects, theyre rarely used, if at all. You may see them in smaller scripts where breaking the encapsulation rules is not an issue.$? - The exit status of the last child process executed. Of all the variables listed here, this is probably the most useful. The reason for this is simple: you cant get the exit status of child processes by their return value from the system method, only true or false. If you must know the actual return value of the child process, you need to use this special global variable. Again, the name of this variable is taken from the UNIX shells.$_ - The last string read by gets. This variable may be a point of confusion for those coming to Ruby f rom Perl. In Perl, the $_ variable means something similar, but totally different. In Perl, $_ holds the value of the last statement and in Ruby it holds the string returned by the previous gets invocation. Their usage is similar, but what they really hold is very different. You dont often see this variable either (come to think of it, you rarely see any of these variables), but you may see them in very short Ruby programs that process text. In short, youll rarely see global variables. Theyre often bad form (and un-Ruby) and only really useful in very small scripts, where the full implication of their use can be fully appreciated. There are a few special global variables that can be used, but for the most part, they arent used. You dont really need to know all that much about global variables to understand most Ruby programs, but you should at least know that theyre there.
Wednesday, March 4, 2020
Captain Sir Henry Morgan - Biography
Captain Sir Henry Morgan - Biography Henry Morgan - Early Life: Little information exists regarding Henry Morgans early days. It is believed that he was born around 1635, in either Llanrhymny or Abergavenny, Wales and was the son of local squire Robert Morgan. Two principal stories exist to explain Morgans arrival in the New World. One states that he traveled to Barbados as an indentured servant and later joined the expedition of General Robert Venables and Admiral William Penn in 1655, to escape his service. The other details how Morgan was recruited by the Venables-Penn expedition at Plymouth in 1654. In either case, Morgan appears to have taken part in the failed attempt to conquer Hispaniola and the subsequent invasion of Jamaica. Electing to remain in Jamaica, he was soon joined by his uncle, Edward Morgan, who was appointed lieutenant-governor of the island after the restoration of King Charles II in 1660. After marrying his uncles eldest daughter, Mary Elizabeth, later that year, Henry Morgan began sailing in the buccaneer fleets that were employed by the English to attack Spanish settlements. In this new role, he served a captain in the fleet of Christopher Myngs in 1662-1663. Henry Morgan - Building Reputation: Having taken part in Myngs successful plundering of Santiago de Cuba and Campeche, Mexico, Morgan returned to sea in late 1663. Sailing with Captain John Morris and three other ships, Morgan looted the provincial capital of Villahermosa. Returning from their raid, they found that their ships had been captured by Spanish patrols. Unperturbed, they captured two Spanish ships and continued their cruise, sacking Trujillo and Granada before returning to Port Royal, Jamaica. In 1665,Ã Jamaican Governor Thomas Modyford Morgan appointed Morgan as vice-admiral of and expedition led by Edward Mansfield and tasked with capturing Curacao.Ã Once at sea, much of the expeditions leadership decided that Curacao was not a sufficiently lucrative target and instead set course for the Spanish islands of Providence and Santa Catalina.Ã The expedition captured the islands, but encountered problems when Mansfield was captured and killed by the Spanish. With their leader dead, the buccaneers elected Morgan their admiral. With this success, Modyford began sponsoring a number of Morgans cruises again the Spanish. In 1667, Modyford dispatched Morgan with ten ships and 500 men to free a number of English prisoners being held in Puerto Principe, Cuba. Landing, his men sacked the city but found little wealth as its inhabitants had been warned of their approach.Ã Freeing the prisoners, Morgan and his men re-embarked and sailing south to Panama in search of greater riches. Targeting Puerto Bello, a key Spanish center of trade, Morgan and his men came ashore and overwhelmed the garrison before occupying the town. After defeating a Spanish counterattack, he agreed to leave the town after receiving a large ransom. Though he had exceeded his commission, Morgan returned a hero and his exploits were glossed over by Modyford and the Admiralty. Sailing again in January 1669, Morgan descended on the Spanish Main with 900 men with the goal of attacking Cartagena. Later that month, his flagship, Oxford exploded, killing 300 men. With his forces reduced, Morgan felt he lacked the men to take Cartagena and turned east. Intending to strike Maracaibo, Venezuela, Morgans force was compelled to capture San Carlos de la Barra Fortress in order to move through the narrow channel approaching the city. Successful, they then attacked Maracaibo but found that the population had largely fled with their valuables.Ã After three weeks of searching for gold, he re-embarked his men before sailing south into Lake Maracaibo and occupying Gibraltar. Spending several weeks ashore, Morgan next sailed north, capturing three Spanish ships before re-entering the Caribbean. As in the past, he was chastised by Modyford upon his return, but not punished. Having established himself as the preeminent buccaneer leader in the Caribbean, Morgan was named commander-in-chief of all warships in Jamaica and given a blanket commission by Modyford to make war against the Spanish. Henry Morgan - Attack on Panama: Sailing south in late 1670, Morgan recaptured the island of Santa Catalina on December 15 and twelve days later occupied Chagres Castle in Panama. Advancing up the Chagres River with 1,000 men, he approached the city of Panama on January 18, 1671. Splitting his men into two groups, he ordered one to march through nearby woods to flank the Spanish as the other advanced across open ground. As the 1,500 defenders attacked Morgans exposed lines, the forces in the woods attacked routing the Spanish. Moving into the city, Morgan captured over 400,000 pieces of eight. During Morgans stay, the city was burned however the source of the fire is disputed. Returning to Chagres, Morgan was stunned to learn that peace had been declared between England and Spain. Upon reaching Jamaica, he found that Modyford had been recalled and that orders had been issued for his arrest. On August 4, 1672, Morgan was taken into custody and transported to England. At his trial he was able to prove that he had no knowledge of the treaty and was acquitted. In 1674, Morgan was knighted by King Charles and sent back to Jamaica as lieutenant governor. Henry Morgan - Later Life: Arriving in Jamaica, Morgan took up his post under Governor Lord Vaughan. Overseeing the islands defenses, Morgan also further developed his vast sugar plantations. In 1681, Morgan was replaced by his political rival, Sir Thomas Lynch, after falling out of favor with the king. Removed from the Jamaican Council by Lynch in 1683, Morgan was reinstated five years later after his friend Christopher Monck became governor. In declining health for several years, Morgan died on August 25, 1688, renowned as one of the most successful and ruthless privateers ever to sail the Caribbean. Selected Sources Cordingly, David. Under the Black Flag: The Romance and the Reality of Life Among the Pirates. New York: Random House, 2006Henry Morgan BiographyData Wales: Henry Morgan
Subscribe to:
Posts (Atom)