Saturday, December 28, 2019

OptionParser Command-Line Options the Ruby Way

Ruby comes equipped with a powerful and flexible tool to parse command-line options, OptionParser. Once you learn how to use this, youll never go back to looking through ARGV manually. OptionParser has a number of features that make it quite appealing to Ruby programmers. If youve ever parsed options by hand in Ruby or C, or with the getoptlong C function, youll see how welcome some of these changes are. OptionParser is DRY. You only have to write the command-line switch, its arguments, the code to run when its encountered, and the command-line switch description once in your script. OptionParser will automatically generate help screens for you from this description, as well as infer everything about the argument from its description. For example, it will know the --file [FILE] option is optional and takes a single argument. Also, it will know that --[-no]-verbose is really two options and will accept both forms.OptionParser will automatically convert options to a specific class. If the option takes an integer, it can convert any string passed on the command-line to an integer. This cuts down on some of the tedium involved in parsing command-line options.Everything is very contained. All of the options are in the same place, and the effect of the option is right along-side the definition for the option. If options have to be added, changed or someone simply wants to see what they do, there is only one place to look. Once the command-line is parsed, a single Hash or OpenStruct will hold the results. Enough Already, Show Me Some Code So heres a simple example of how to use OptionParser. It doesnt use any of the advanced features, just the basics. There are three options, and one of them takes a parameter. All of the options are mandatory. There are the -v/--verbose and -q/--quick options, as well as the -l/--logfile FILE option. Additionally, the script takes a list of files independent of the options. #!/usr/bin/env ruby # A script that will pretend to resize a number of images require optparse # This hash will hold all of the options # parsed from the command-line by # OptionParser. options {} optparse OptionParser.new do|opts|   Ã‚  # Set a banner, displayed at the top   Ã‚  # of the help screen.   Ã‚  opts.banner Usage: optparse1.rb [options] file1 file2 ...   Ã‚  # Define the options, and what they do   Ã‚  options[:verbose] false   Ã‚  opts.on( -v, --verbose, Output more information ) do   Ã‚  Ã‚  Ã‚  options[:verbose] true   Ã‚  end   Ã‚  options[:quick] false   Ã‚  opts.on( -q, --quick, Perform the task quickly ) do   Ã‚  Ã‚  Ã‚  options[:quick] true   Ã‚  end   Ã‚  options[:logfile] nil   Ã‚  opts.on( -l, --logfile FILE, Write log to FILE ) do|file|   Ã‚  Ã‚  Ã‚  options[:logfile] file   Ã‚  end   Ã‚  # This displays the help screen, all programs are   Ã‚  # assumed to have this option.   Ã‚  opts.on( -h, --help, Displa y this screen ) do   Ã‚  Ã‚  Ã‚  puts opts   Ã‚  Ã‚  Ã‚  exit   Ã‚  end end # Parse the command-line. Remember there are two forms # of the parse method. The parse method simply parses # ARGV, while the parse! method parses ARGV and removes # any options found there, as well as any parameters for # the options. Whats left is the list of files to resize. optparse.parse! puts Being verbose if options[:verbose] puts Being quick if options[:quick] puts Logging to file #{options[:logfile]} if options[:logfile] ARGV.each do|f|   Ã‚  puts Resizing image #{f}...   Ã‚  sleep 0.5 end Examining the Code To start off with, the optparse library is required. Remember, this isnt a gem. It comes with Ruby, so theres no need to install a gem or require rubygems before optparse. There are two interesting objects in this script. The first is options, declared at the top-most scope. Its a simple empty hash. When options are defined, they write their default values to this hash. For example, the default behavior is for this script to not be verbose, so options[:verbose] is set to false. When options are encountered on the command-line, theyll change the values in options to reflect their effect. For example, when -v/--verbose is encountered, it will assign true to options[:verbose]. The second interesting object is optparse. This is the OptionParser object itself. When you construct this object, you pass it a block. This block is run during construction and will build a list of options in internal data structures, and get ready to parse everything. Its in this block that all the magic happens. You define all the options here. Defining Options Each option follows the same pattern. You first write the default value into the hash. This will happen as soon as the OptionParser is constructed. Next, you call the on method, which defines the option itself. There are several forms of this method, but only one is used here. The other forms allow you to define automatic type conversions and sets of values an option is restricted to. The three arguments used here are the short form, long form, and description of the option. The on method will infer a number of things from the long form. One thing is will infer is the presence of any parameters. If there are any parameters present on the option, it will pass them as parameters to the block. If the option is encountered on the command-line, the block passed to the on method is run. Here, the blocks dont do much, they just set values in the options hash. More could be done, such as checking that a file referred to exists, etc. If there are any errors, exceptions can be thrown from these blocks. Finally, the command-line is parsed. This happens by calling the parse! method on an OptionParser object. There are actually two forms of this method, parse and parse!. As the version with the exclamation point implies, it is destructive. Not only does it parse the command-line, but it will remove any options found from ARGV. This is an important thing, it will leave only the list of files supplied after the options in ARGV.

Friday, December 20, 2019

The Crime Of Brazil And Brazil - 1698 Words

Brazil has some of the most violent cities in the world. The murder rate in Brazil has been going through the roof each year. Some cities have a murder rate of 50 per 50,000 people. Most murder victims are usually male, poor and teenagers. Most of these crimes take place in the infamous favelas of Brazil. Murder is not the only crime leaking in the shallow streets of the favelas. High-profile rape cases, kidnapping, drugs, and weapons dealing in the favelas are the common headlines in Brazil (Griffin, Jo). These crimes are born in the favelas and spread through the streets of Brazil. The economic growth of Brazil over the years has benefited the rich puppeteers of the society, but it has made life more miserable for the poor (McCann, Bryan). Not just in Rio, but favelas have spread throughout many cities in Brazil and now they are the number one birthplace of different types of crimes in the country. In this paper, we will analyze an article published on the online version of ‘ The Guardians† regarding the lives of people living in Brazilian slums (favelas). The article talks about the life inside these favelas. For tourists, these slums may be seen as a place of unique interest, but to the Brazilian people, these slums are the most dangerous places in the country. But the question arises that why can t the government do anything about these slums. The article describes that these favelas are so dangerous that even the Brazilian armed police are often scared to launch anyShow MoreRelatedThe Economic Crisis Of Brazil1395 Words   |  6 Pages Brazil Historical Development While Brazil development state began in the response of the Great Depression during the economic crisis. The second section of this report describes the formal protection of human rights in the Brazilian criminal justice system, but also explains why these guarantees remain largely on paper. An understanding of why the Brazilian state appears to violate so many of the human rights that its own laws and Constitution guarantee requires some description of the historicalRead MoreFederal Republic of Brazil1653 Words   |  7 PagesFederative Republic of Brazil. Brazil is the largest country in South America, it is located in Eastern South America and it borders the Atlantic Ocean. Brazil borders many countries including Argentina, Bolivia, Columbia, French Guiana, Guyana, Paraguay, Peru, Suriname, Uruguay, and Venezuela. Brasilia is the capital and it is located in the Brazilian highlands in the country’s Central West Region. Brasilia was founded on April 21, 1960 and since then the population of Brazil has grown to about 201Read MoreEssay about Crime in Latin America1197 Words   |  5 Pagesbeing treated from the beginning. Crime has become a big problem during these hard times with the poor economy, but it has especially affected Latin America because of all the problems that overcrowded prisons have brought forward. In Latin America Brazil and Mexico are the two largest countries that have been affected with having the highest percentage of crimes, inmates in prisons, and concerns with overcrowded prisons. And these increase with the high crime rates in Latin America that are risingRead MoreBrazil s Independence Of Brazil996 Words   |  4 PagesThe people of Brazil gained independence from Portugal on September 7, 1822, and became known as the Federative Republic of Brazil. With European, African, and Indigenous peoples influences, Brazil has become a melting pot of ethnicities, religions, and cultures. It is made up of 26 states and has a total p opulation of 205,823,665 people. The two largest ethnic groups are White at 47.7 percent and mulatto (mixed white and black) at 43.1 percent of the population. Most of the population lives onRead MoreBad News Can Rebuild Brazil1254 Words   |  6 PagesMarina Mendes Lemos de Oliveira Jerry Martin ENG 108 May 1, 2017 WA9: Bad News Can Rebuild Brazil The fight against corruption is a considerable issue that has been dominating all the news and informal conversations in Brazil for the last two years. The country is again in the midst of a whirlwind of public scandals. In Brazil, it is very common to associate corruption with politicians. This happens because almost every day newspapers and television present complaints about the abuse of public resourcesRead MoreDaniela Khodorov. Core Global Issues. May 1, 2017. Human1352 Words   |  6 PagesDaniela Khodorov Core Global Issues May 1, 2017 Human Trafficking in Brazil According to statistics from the United States Department of State, there are reportedly over 13 million people being forced into human trafficking, specifically sex trades. Sex trafficking is one of the most profitable crimes making 7 to 12 billion dollars yearly around the world (Research Paper). This is also often times referred to as â€Å"modern day slavery† as adults and children are exploited and stripedRead MoreAnalysis Of Rio De Janeiro s Most Infamous Favela1590 Words   |  7 PagesRio, is the second most populous city in Brazil and the sixth largest city in America; the city is known for holding hundreds of favelas where low-income residents reside. According to the findings of a 2010 census, about 22% of 6 million residents live in the favelas of Brazil (Hurrell).These favelas are known for their brutality and crime rate among the residents. The quality of life in the Brazilian favelas has in a way improved and also worsened. As Brazil is set to hold the Summer Olympic GamesRead MoreThe Legal Aspects Of Brazil1329 Words   |  6 Pagesaspects of Brazil are as large and diverse as their population. Brazil is currently a Federal Republic. Similar to the United States, it has three legislative branches, the executive, legislative and judicial (â€Å"South America: Brazil†, n.d.). Brazil also has a constitution that sets the rules for its government and citizens, and is very similar to the United States Constitution. The government has high tariffs put into place to encourage other countries to bring their manufacturing jobs to Brazil, insteadRead MoreThe Legal Aspects Of Brazil1332 Words   |  6 Pagesaspects of Brazil are as large and diverse as their population. Brazil is currently a Federal Republic. Similar to the United States, it has three legislative branches, the executive, legislative and judicial (â€Å"South America: Brazil†, n.d.). Brazil also has a constitution that sets the rules for its government and citizens, and is very similar to the United States Constitution. The government has high tariffs established to encourage other countries to bring their manufacturing jobs to Brazil, insteadRead MoreThe Policing Model : Brazil1160 Words   |  5 PagesThe Policing Model Brazil In Brazil there are five different police enforcements, the federal police, he federal highway police, the federal railway police, the state military police and fire brigade, and the state civil police. Brazil police officers only have two main functions which is to keep order and to make sure laws are not broken. The Federal Police: The federal police main function is to investigate crimes that are against the federal government, to fight international drug trafficking

Thursday, December 12, 2019

Music and Middle Ages Essay Example For Students

Music and Middle Ages Essay What are the Hurricane songs? Why are they important? -Its a collection of music ornamented in cuneiform in a clay tablet. Back then , It was important to teach young males or females how to play then today. 2. What is the Natty Shasta? Why is it important? -Its an Indian treat that perform art, surrounding music , theater , and dance. For Indians since its a performance, Its important to show culture and what type of music they listen to. 3. Who were troubadours? What was their music like? -It was a group of composers and performers of a poetry around the era 1100-1350 in he Middle Ages. Their type of music is infamous and diabolic songs of love. 4. What are the modes in Western music? Describe at least one mode in Western music. They are a series of pitches that are commonly used to play music. One example would be Lydia. 5. What is polyphonic music? How does it differ from monophonic music? -Is a music that uses one or more independent melodies. The differences were Monophonic was conquered in the starting of the middle ages and in other hands , Poly. Was well-liked and was used more often. Critical Thinking Questions 6. Why did early humans develop music? What are some of the ways that they were thought to create music? -The Early humans increased the music to a mimic tone and sound they heard. It was to help along side with humans and Most of the ways they brainstormed to make create or make music was using their voice. 7. Why is the study of prehistoric and ancient music important? What can we learn from it? -It was important because they mostly uncovered evidence that was connected to music and how music was influenced by them today. Music and Middle Ages By Mister-Ajax c:

Wednesday, December 4, 2019

The Kurds free essay sample

The writer takes the reader on an exploratory journey through the Kurdish history, as well as an examination of current situations. A detailed analysis of the Kurdish question following the Gulf War. The writer takes the reader on an exploratory journey through the Kurdish history, as well as an examination of current situations. The national movements, the history of the problem and other aspects are looked at in close detail and tied together to present a systematic research approach to the Kurdish question. The people themselves are building a national identity however. They sing national songs, they have a language that belongs to their ;nation, and they even have a currency that is exclusively Kurdish. In Kurdistan children are taught the history of their people and they are taught to have national pride. Like the person who lives out of hotels, but still is alive, the Kurdish consider themselves a nation without a home. We will write a custom essay sample on The Kurds or any similar topic specifically for you Do Not WasteYour Time HIRE WRITER Only 13.90 / page The national identity has never been dependent on having land to call their own as they moved from place to place trying to find a permanent residence.