Martin Shanghai speech transcript 3: detailing the advantages and disadvantages of Ruby

Source: Internet
Author: User

Advantages and disadvantages of Ruby

It seems a bit cool to explain what these are actually doing. But what I want to pass on is the experience when using Ruby. Although you cannot try it yourself, you can see the process on the screen. Now I want to show another example. I hope you can see some experience in the content. If you haven't tried any scripting language before, I encourage you to learn Ruby. I think Ruby is worth learning. Because it is easy to learn and use, it takes only a few minutes to learn. Another feature of the scripting language is that you can write some code and then directly view its running results. Ruby is a very good scripting language. However, I think the significance of Ruby is not only that it is a good scripting language. I will discuss this issue in the next few minutes.

First, I hope to discuss this topic through Ruby features. You may have learned this from the example. Ruby is a very concise syntax, so that you don't need to write a lot of code, you can get a very powerful function. From the example above, we can see that when writing parameters, you do not need to write parentheses. Of course, you can also have no parameters at all. This feature allows you to quickly write code, but more importantly, you can quickly read the code. We can compare the Java and C series languages. When you try to read the code, your line of view is always dispersed and it is not easy to understand what the code is doing. Loose type check can help us understand code faster and better. Strict type checks can cause worse readability. Ruby has made a good balance between them. Code readability is of great value for both Ruby and Ruby developers. I think the code written in ruby is quite readable. This does not mean Ruby is a very small language, just like smalltalk. Ruby is a complex language. It has many complex syntax features, such as implementing multiple tasks in the same line of code. If you have used other scripting languages, you will find that both Perl and Python features are included in ruby. This makes Ruby learning more difficult. This is a new balance. In a simple language, it is complicated to express things, such as Smalltalk. However, in ruby, it is very convenient, especially in controlling collection. In Ruby, the list syntax is very convenient. I didn't show how to use hashtable in Ruby, but they are also simple to use.

A bit of attention is received. Unlike mainstream static types such as Java and C #, you do not need to define variable types in ruby. It has a strict type. Remember the example in the experiment just now. If the string type is used for subtraction, an error is prompted. However, you will be notified of errors only during running, rather than compiling. Therefore, Ruby has strict types. However, its type is dynamic. So you can only find errors at runtime. This leads to the following result. Many people worry that it is difficult to find a type error when writing Ruby code, this is not as expected in Java and C. However, I found that this problem does not exist in most developers who use Ruby. They solve this problem through testing, especially unit testing. I am a supporter of the test. I will use the test a lot, not in the current small demo program, but in the previous slightly larger project. If you use unit tests, you will find that errors of the type are as fast as errors found during compilation. I like this feature very much, especially when I first used Smalltalk to build a large system. Type errors cannot be found in tests only in rare cases. However, there is a drawback in such a language. When you read the code, you cannot easily understand what the program has done, this is because you cannot know the type of the variable when controlling it. This means that Ruby cannot have some powerful debugging tools like smalltalk, so that you can step into the running program in the debugging environment and see the variable type. In general, I like the flexibility brought about by dynamic languages. Objects can be replaced, and can be used as various types in many places. But on the other hand, it improves the readability of the Code. It is easy to understand when you try to read the code. This depends on your personal preferences. Some prefer static languages and some prefer dynamic languages. Therefore, you need to select a preferred language. Many people think that category is very important, and I think this is not decisive.

I think another feature of Ruby is its strong object-oriented feature. Perl is not an object-oriented language, although it was later added with some object-oriented mechanisms. Python is not completely object-oriented. But ruby was object-oriented at the beginning. Here I can show another piece of code. (Write a piece of code ). Therefore, in ruby, even nil has a class, and nothing is an object. This is like smalltalk. The difference between an object and a basic build type is that in ruby, objects are all. Although some of my previous Code is that you did not see the initialization code. Ruby is built in this way. Here is another good example. (Write a piece of code ). We can see that here we get some big integers. Can you implement it with such a simple statement in Java? No, because you need to consider how to handle it when your value increases to a certain extent. Consider things like increasing the number of digits. It is interesting that Ruby does this in the background, but we cannot see it. Now let's output its type to see what will happen. (Write code) to center your output. Here we can see that the numbers are of the "fixnum" type at the beginning and are small integer classes. When it grows to a large enough value, it needs to become a relatively large type like in Java. Therefore, the specific implementation class can be displayed here. This is the advantage of Dynamic Language. Smalltalk can even be better. It can convert the number type to the score type and the floating point type according to your needs. Therefore, Ruby has a very good object-oriented background. In such a script environment, your program can have a better structure. You can have a better speed, mechanism, and structure, so that you can write better structural programs. This is the result of continuous integration.

However, not everything in ruby is good, but it has its shortcomings. One of the most important problems is that it does not have a very good development tool, especially after you are familiar with its editor, it cannot be a development environment like Java, such as Eclipse. and my favorite intelligent J, which provides smart prompts to guide programmers to write code. These do not exist in the Ruby community. I currently recommend Emacs. This is definitely a disadvantage for people. These depend on how much resources people actually invest. Another problem caused by this is that we cannot refactor like in Java and C. It is difficult to refactor dynamic languages. This is not absolute. The first refactoring feature is smalltalk, but it is not perfect.

Another problem with Ruby is that Ruby's block time is not fast. In the scripting language standard, it is a slow language. However, this is not a serious problem. In our programs, Ruby is used to call databases. Ruby has better performance than database connection and some hardware functions. But ruby is not the best in other aspects. Ruby APIs are built into the C library, which is a common situation, such as Perl and python.

Another disadvantage of Ruby is that it is terrible to process Unicode. Ruby. The program I just demonstrated is actually a string of the acⅱ type. You cannot use Unicode for code as many people do. It is not as easy to use as it is. This problem will be solved in the next version. However, at least for the moment, Ruby cannot support Unicode as well as Java and python.

Tools and DSL create Ruby charm
The most interesting and important part is its library, framework, and web development framework. In some of our projects, Ruby on Rails is a highly productive web development framework. An important issue when developing enterprise applications is that database code and other code are combined. You must control the code to make database requests valid. This becomes an important issue when databases are managed by different departments of another company. If you have some control over the database, your environment will become messy. Rails is designed specifically for some types of applications, rather than to solve all the problems. For example, Internet applications have special advantages.

Another attractive library in ruby, which I personally think is very useful, is that it can manage every build version. A well-known Unix environment is the ability to build C-language programs. Rails helps us describe a build. I will display this feature in a small window, which will be very interesting. Here is the build script on the website. Here I define a task and dependency. The default task depends on all. All tasks. Here is the logic code of the task. It will execute this code. Let's look at the help function and the clean task. People familiar with Ruby code will be familiar with what they are doing. The task here is the definition of the method. In default, you can see an arrow pointing to all, you can find the corresponding in a hashtable. Closure is used here (Note: The anonymous method mentioned above ). Therefore, special build languages are of special use. The Build File has its own special attributes. You want to list the branch structures and dependencies of all tasks. Anyone wants to obtain the dependency between tasks, so tag to mark that task must be executed. I define these tasks in Ruby code. Build these tasks in the memory, and then the necessary tasks will be executed. Building my website means constructing 700 files. However, I can know which file has been modified and which one needs to be re-built, and then perform the Minimal build to get the desired result. We found this very interesting. It can record a small amount of historical information. This type of earlier language is the make language, which is usually used in UNIX systems. Make is a special file format used to describe how to build code. It is a special domain language and can be used to build applications. It has its own format and syntax to describe how to build a C language program. Make has a problem, which means it is not easy to learn. It is hard for you to understand the automatically generated makefiles. However, many programs have the ability to automatically generate make files, because it is not easy to master if you write the files by yourself. Java has no build tools at the beginning. So James Duncan David son wrote ant for Java construction. The authors of ant don't want the build to involve syntax issues. They use XML as the build script. I was not complaining about this problem. XML was a good choice at that time. There are two problems with using XML syntax. Because the syntax is very strict, you need to spend a lot of energy to focus on the syntax, so it becomes difficult to understand the operations it performs. Another problem is that when the system grows larger, the build file contains a lot of logic, which makes it difficult to create an ant file. So when the system becomes very large, it is very difficult to create an ant file to build the system. We need to find a way to release from XML to build these classes.

Ruby is such a powerful language that verifies Ruby syntax when there are certain syntax restrictions. Ruby does not introduce too many New syntaxes. This looks good, because you need a new language to implement this function. But you still have a limitation that you cannot use it outside Ruby. But another benefit is that you can use Ruby When You Need To Do complicated things. Therefore, you have more powerful capabilities to accomplish things. This external language allows you to use its syntax and gradually become more capable of coping with it. Because it can help you compile more and more complex times. This capability is easy to obtain. Its syntax is very direct and you can easily learn it. It is a traditional idea to use such a language as the host language. But this is very useful.

Sometimes it is not easy to make Ruby attractive to users. I have been a ruby enthusiast for several years. I am committed to promoting it for our work. I told others that I like Ruby and encouraged others to use it. However, using or not using a product is not very important. I do not recommend all projects. However, we have used Ruby in many projects in the last six months. We found that ruby may not be suitable in some projects, but Ruby is very suitable for other projects. The core of this lies in the fact that successful cases prove that such projects are really suitable for Ruby. Although rails provides a large framework, it is almost impossible to succeed without Ruby. This is one of the reasons for project success.

My colleague is working on a project that requires a lot of database operations. It is unnecessary to extract the data from the database, calculate the data in the memory, and then put the data back into the database. You want to use SPL to perform these operations. The use of SQL is limited. It is powerful but not easy to use. A major problem with SQL is that it is completely based on domain. It can perform good operations on table format data, but it does not have the ability to abstract the table. So if you want to operate the table, this is very good. However, if you want to perform other operations, it will be very troublesome. A typical example is that a database with a hierarchical structure is complex and it becomes complicated and difficult to operate with SQL. In this project, the ruby language is used to allow the input of domain specific language Ruby, and SQL code can be generated to operate the database. Such a special language is very useful. Experts in the field can read and understand the code without thinking about the syntax and how to generate the code. You can focus on other places. Domain specific language like Ruby makes it very attractive.

The Ruby community is exciting.

Now let's take a few photos to summarize the speech. Ruby is an interesting system. I can't say it is suitable for anywhere, but it is indeed suitable for many places. DSL is its core. Some specific types of websites, especially those that need to use databases, also use the rails framework. Almost all administrating work and scripting work are suitable for Ruby. You can start to use Ruby because it is a very powerful tool. Ruby is the best scripting language I have ever seen, and you will have the same experience. In terms of building, Rake (Note: Ruby's build tool) is a very good build tool. About six months ago, I replaced my website with all the files that were built and maintained using it. I think you will never regret it, because it is very easy to build and maintain these build files.

Before I finish my speech, I have the last thing to do. When talking about software development, I have always stressed that no matter what tools or practices you use, such as Pair Programming and test-driven, the most important factor about the success of a software development project is the people working in the project and how these people work together. In the Agile Software declaration, the primary value is that processes and tools are more important and more valuable than individual and intercommunication. The reason Ruby deserves attention is not that its features are not its framework, nor the productivity improvement it brings in rails, but the work of Ruby community members. Dave Thomas (author of the book <pragmatic programmers>) has mentioned this many times, which is the place where it attracts me. I have been very interested in the last year 23 years. The Ruby community attracts more and more experts to join and make continuous progress. Another advantage is that open-source Ruby works can be directly installed through the network. If you want to use ruby, it is very easy to install it through rubygem-this is a package management system. (Note: it is like debain's Unix package management system ). The Ruby community is growing rapidly, because excellent members are constantly joining this team and working in this environment, which makes this team very noteworthy. This is also the main reason why I have been paying attention to Ruby because many people are working in this community.

This is my first speech on Ruby. I hope it is not an incoherent speech.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.