It is already Martin, which has experienced Ruby programming with the audience.
Live demo Ruby programming
How many people have we used Ruby to develop? Very surprised, just someone else. So many people have not seen ruby yet, this is the reason why I took notebook. I will demonstrate what we can do with Ruby. In this case, we will have a understanding of this language. I now put the microphone while putting my laptop.
Ruby is an immediate execution scripting language. Next, create a new source code file in the editor environment.
The window of this console will not be used frequently, don't worry, I can type the name of this file and run it. I can also do this in the editor and print it in other files. We are now doing a small program. Here is some of the data charts, imagine it into charts of each country, including: Fail Graphics, Operating Figures, and more. These may be something that the business is paying attention to every day. Now we will imagine the first imagination, the second imagination is expected to be expected, third, imagine into actual situation. So now these business people have put forward a request to print out these data and show the percentage of the difference between the expected value and the real value. So we take some time to complete this work.
The first job we have to do is to read this file and display it. How do I do a slower display at the beginning. (Input Code) This will be a relatively simple version. It is now able to display the contents of the file. I will explain the syntax here. There is a FILE class here, there is a method of open. Here, there is no parentheses outside the parameter, but if you need it, you can add it here, the program can still work. Brackets can be selected in Ruby programming, which looks not a very important grammatic point, but many times, this is very important, such as in the next encoding, or some other scripts. Here, there is a very simple While cycle. We have a local variable line. Get this variable with Getline. Remember, only to declare this variable when you need it, we don't pre-declare. After we output this line, then close the file.
However, I have already said, this is not a way to implement this function with Ruby. Usually, you will do this. Here is an error here. Ruby has a very good help system. We look here where the error is there. I really can't remember. However, you can see it before. Such programming is very dangerous, because you need to know what you are doing, while expressing what you are doing. This is very difficult, I am very small. So here, this thing I am doing is very late in this language (Sophistated). Here is an operation for each row of the file. This part is the CLASURE of the file. Here is to execute this very short code between DO and End. When each execute command, it is executed once. Passing a code as a parameter into a Function, which is very common in programming languages. This is the case in SmallTalk, then in Java is also an anonymous method. The agent in C # is fundamentally this. Closure is a very powerful feature of Ruby. If you often use Ruby, you will find that this closure has a very much application. The advantage of using this way here is that it can perform this operation every line of each file you pass to the file name, and turn the file after execution. If you don't need this method, you have to turn off the file each time, which has formed a series of transactions. When you do one thing, you will do the next piece. This problem can be avoided using this anonymous method. The next thing to do is put this line to the output into an object. Create a class here, the name is Performance. Now establish the figure we need, the name is attr_assessor, then define the Name Amount and Actual, which actually defines three values we need to display. Here I can change the code in my loop. P = performance.newp.name = line.split (') [0] The line can be separated by a comma, then we get the first one. After that, we output P, here we see the output is the P this object itself, not the value we want. In Java, we can define a string type. But in Ruby, we define a DEF TO_S function to return it to the value of Name. Now, in the output of the output, we see the name we really want. So, we can see that it is very easy to build a class we need. Next, we can change this class to a true object-oriented style. We need the results to return an array. The READ method is to return to the result. (A series of code movements, debugging, forgetting to add P to the Result array) This will get the correct result. We have some values that need to be placed in our class. Through this, we can achieve it. Here Line.Split (',') will return an array. This code has performed a composite operation. Each variable corresponds to a different value in the array. This is a feature of the scripting language. So, add Plan in a function that returns a string, plus actual. Review the result again, we can see all the values we want.
It is very organized now to see our code. Then we can join the computing function. Define a Variance method. Return (Actual -PLAN) / Plan * 100, so we got a percentage. (Write a series of code) At this time I can determine that the code cannot be run because these values are still character types (written a series of code). All scripting languages have a common problem: there is no abnormal mechanism. However, you can handle errors in other ways. Now let's take a look at the results, I got the limit value. Here is because I forgot to join the floating point. The result of this is correct. However, the output we get is hard to see. We are about to use Ruby's formatted mechanism. (Write some code to make the output into a decimal point.) Output the country name so that we can see the complete display. In this code that format string output, it can be formatted in the C language. There are still some problems now, but we have to continue to see the next part. Sort in Perfs. This is sorted by VARIANCE. (Write some code) The result is now displayed in accordance with Variance. The most important thing we need to know is this line of code. This is the use of Closure in Ruby. I put the array in Raw_Perfs, which was sorted in a special domain and then conducted another operation. Operation over the collection. In Ruby, this operation of Collection makes Ruby a very simple language. Whether it is for SMALLTALK or a List, it is similar to the operation of the object. However, when you haven't used this language, it doesn't usually look. But once you start using this feature, it will become very powerful because you can easily use your META to operate.