A summary of Ruby's basic syntax and a summary of ruby's basic syntax
1. Keywords
Keywords cannot be used to define variables or constants, module, class, def, undef, defined ?, If, then, else, elsif, case, when, unless, for, in, while, until, next, break, do, redo, retry, yield, not, and, or, true, false, nil, rescue, ensure, super, self, begin, end, BEGIN, END ,__ FILE __, _ LINE __, return, alias
2. Notes
A single line comment starts with #, for example, # This is a comment statement.
Multi-line comments start with = begin and end with = end, for example:
Copy codeThe Code is as follows:
= Begin
Block_test = lambda {puts 'This is from lambda! '}
Def block_foo (bt)
Puts 'before yield! '
Bt. call
Puts 'after yield! '
End
Block_foo (block_test)
= End
3. Data Type
In Ruby, the following data types are available: Numeric (including Fixnum and Float), String, Boolean, Array, Regexp, Range, and Hash. A special type is Symbol.
4. Assignment and conditional operations
The value assignment in Ruby uses "=" to assign values in batches, such as a, B, c = 1, 2, 3.
Conditional operations in Ruby are listed as follows:
Operator |
Description |
= And! = |
Compare whether the values of two objects are equal, a = 1, B = 1.0, a = B (true) |
Eql? |
Are the values of the two objects equal to the data type, a = 1, B = 1.0, a. eql? B (false, a is Fixnum type, B is Float type) |
Eqlal? |
Are the addresses of the two objects in the memory consistent? a = 1, B = 1, a. eqlal? B (false, which is two objects in the memory with different addresses), a = 1, B = a, a. eqlal? B (true) |
<=> |
Compare the values of two objects and return 1 (greater than), 0 (equal to),-1 (less) |
>,>=, <=, < |
Returns true and false to compare the values of two objects. |
=== |
Range includes Relational operators, and whether the object on the right is within the range on the left |
= ~ And !~ |
Regular Expression matching operator, indicating matching and non-matching Regular Expressions |
5. output and input
The IO Input and Output Methods Self-Contained in Ruby are written in the Kernel module, and Mix-in is in the root class Object. The input and output methods can be directly called in any class,
Sample Code of the output method:
Copy codeThe Code is as follows:
Print 'Hello! '# Output: Hello!
Print "Hello! \ N "# output: Hello!
Printf 'Number: %. 2f, String: % s', 14.547, 'Hi! '# Output: Number: 14.55, String: hi!
Print "\ n"
Puts 'Hello! ''Hi' # output: Hello! Hi
Puts 'Hello! ', 'Hi' # output: Hello! [Line feed] hi
Print can be output to a file by adding parameters. By default, it is output to the console. printf is output by format. In the above Code, %. 2f indicates that the first parameter is output in Float type, with two decimal places retained. % s indicates that the second value is output in String type. print "Hello! The difference between "\ n" and "print 'Hello \ n' is that" "is used to parse the output content, and \ n is used to wrap the output content, \ n is output as it is, and does not have a line feed function. The parameters of the puts method are variable parameters. There can be zero or multiple parameters. If there is a space between parameters, the output will not wrap, and ", ", and each output parameter will have a line break;
The input method. gets is used to receive user input strings. A line break is automatically added at the end of the string. Therefore, when the gets method is used to obtain input data, the chomp method is used to remove the line break at the end.
Copy codeThe Code is as follows:
STDOUT. flush
In_data = gets. chomp
6. Condition judgment
If else, unless, and case can be used for condition determination. different keywords can be used in different cases to make the code more concise and easy to understand. The following is an example of code:
Copy codeThe Code is as follows:
R = r and * 100
Rf = format ('%. 2f', r)
# Normal writing
If r> 50 then puts "score: # {rf}" end
# Better Writing
Puts "score: # {rf}" if r> 50
# When judging if not, use unless. The next code is equivalent to the previous one.
Puts "score: # {rf}" unless r <= 50
# Multi-condition judgment
If r <50
Puts "score: # {rf} score: unqualified"
Else
Puts "score: # {rf} score: good"
End
# More Conditions
If r> 90
Puts "score: # {rf} score: Excellent"
Elsif r> 70
Puts "score: # {rf} score: good"
Elsif r> 50
Puts "score: # {rf} score: qualified"
Else
Puts "score: # {rf} score: unqualified"
End
# A better way is to use the case Branch statement. If Range is used for the condition after when, Ruby automatically uses = for judgment.
Case r
When 90 .. 100
Puts "score: # {rf} score: Excellent"
When 70 .. 90
Puts "score: # {rf} score: good"
When 50... 70
Puts "score: # {rf} score: qualified"
Else
Puts "score: # {rf} score: unqualified"
End
7. Loop
Ruby has powerful loop functions. In addition to common while, until, for loops, each, times, upto, downto, and step loops, Ruby uses different loop methods for different loop objects, the exception is convenient. The sample code is as follows:
Copy codeThe Code is as follows:
I = 0
# Normal while loop
While I <10
I + = 1
Puts I
End
# While loop of a single sentence code
Puts I + = 1 while I <10
# Until loop of a single sentence code, until = while not
Puts I + = 1 until I = 10
J = 10 .. 20
# Use for to perform a full loop of objects. objects can be arrays, ranges, maps, and other collection forms. However, Ruby does not recommend using for loops too much and can use each for replacement, because the for loop does not have a new scope, the Defined variables can be accessed in the external body of the loop. The variable t in the following side loop can be accessed outside the for loop.
For t in j
Puts t
Break if t = 17 # When the cycle reaches t = 17, the entire loop jumps out.
End
# Better Way: Use each for Traversal
# Use a single line code block {}
J. each {| e | puts e}
# Use do end for multiline code blocks
J. each do | e |
Next if e = 17 # if e is equal to 17, the loop jumps out and continues the loop where e is equal to 18.
Puts e
End
# Times
5. times {| e | puts e} # The e value starts from 0.
# You can use upto or downto to traverse ordered sorting.
'A '. upto ('Z') {| e | puts e unless ('H '.. 'N') === e} # traverses English characters between a-z. When e is not letters between h-n
9. downto (1) do | e |
Print "# {e}'s Square Value: # {e ** 2}, # {e}'s power: # {e ** 3 }"
Puts
End
# Step can be used for incremental cycles with the same step size
1. step (100,3) {| e | puts e}
What materials are needed to learn Ruby?
Programming Ruby (2nd Edition)
This does not seem to be a strange thing: the author is not the creator of a classical textbook on a programming language. Just like Stan Lippman in C ++, Joshua Bloch in Java, and Martin Fowler in UML, dave Thomas may be the best person in the world to explain the Ruby language to others-at least more than Matsumoto. Maybe it is because you have also experienced a learning process that you don't understand. Sometimes, "onlookers" are more clear about what learners need than "Creators.
Therefore, this book is a classic tutorial of Ruby. The first part and the second part of the book detail the basic syntax and common tools of Ruby. The third part, "Ruby Crystallized", elaborated on some details and design concepts of the Ruby language. In chapter 23rd, "Duck Typing" was just developed from Java or. NET platform, because the understanding of types and contracts, understanding of classes and types, it is one of the biggest differences between Ruby and other static languages such as Java/C. The subsequent part 4 provides the quick query manual for the Ruby basic library.
Dave Thomas and Andy Hunt, the two "Pragmatic Programmer" are not a waste of name: although this Programming Ruby is not a good reference manual, it is enough to help a beginner step into the Ruby world without going astray, and in rare cases-for example, forgetting yield usage-helps experienced Ruby programmers. In my opinion, this is enough to lay the foundation for being a classic textbook. Because there is a pickaxe on the cover, this book is also known as the "opening book"-it is a necessary tool for you to discover the "Ruby" treasure.
Agile Web Development with Rails
David Heinemeier Hansson, Rails author, told the truth: "I have never learned a language to learn a language ." Most people learn a new language most of the time not to compare the advantages and disadvantages of a language, but because a tool under the language can help their work. This "killer application" in the Ruby world makes Ruby a focus in just one year, namely Rails.
This is the first book to introduce Rails. It is also written by the Rails author DHH and Dave Thomas. Its value is self-evident. Maybe the two authors have too many "items" to hand over to the readers. The first version of this book was written to 558 pages by them, unfortunately. The book first shows a small online shopping website, allowing readers to experience the agile development experience with Rails; then, we discussed the components, security, deployment, and other extended topics of the Rails framework in depth. Its content is comprehensive and in-depth, which is amazing. Unlike Matsumoto, DHH knows exactly how to introduce his work-whether it is "simple" or "in-depth ".
Chinese readers are happy that the first version of this book has been translated by Lin zhixun and Fu Zi, the Electronics Industry Publishing House. Rails is still in a stage of rapid development. Since the first draft of this book, Rails has undergone great changes, so this Chinese translation has become outdated as soon as it is available. However, this book, after all, is not a reference manual. The author mostly describes the design concepts and best practices of Rails. For readers who cannot reach the fastest speed in English Reading, this translation cannot be a competent guide.
Two Rails developer assistants
It's hard to imagine that there are a lot of impatient programmers -- just like me -- after learning about Ruby syntax, They plunged into the beautiful palace of Rails to experience the sense of accomplishment of rapid web application development, but I have to always be confused by lack of in-depth understanding of the Ruby language: there is nothing in this class. Why does it work? That place... the remaining full text>
If you are experienced in Ruby, can you recommend a relatively good tutorial? Now
Programming rubychinese Edition
:
Ishare.iask.sina.com.cn/f/7852164.html? From = like
Ishare.iask.sina.com.cn/f/7852169.html? From = like