Some languages require a certain type of punctuation, typically a semicolon (;) to end each statement of the program. Ruby uses the shell's SH and csh convenience. Multiple statements in a row are separated by semicolons, but not at the end of a line; a newline is treated as a semicolon. If the line ends with a backslash (\), subsequent lines are ignored; This allows your individual logical rows to span several lines.
Comments
Why write a comment? Although good code can be self documented, but the kind of self-thinking it is wrong for others to understand and learn quickly in your way. In addition, you will be another person after a few days away, and after a while we forget what parts of the program we haven't patched or enhanced, and you say, I know I wrote this, but what am I writing about?
Some experienced programmers would quite rightly point out that contradictory and outdated annotations are better than none. Of course, having annotations does not mean that the code is readable; If your code is not clear, it may be a worm. When you learn Ruby, you will find yourself needing more annotations; and then when you can get through simpler, more elegant, Readable code to express ideas, they are reduced.
Ruby follows some common writing habits, using the pound sign (#) to indicate the beginning of the comment. Follow the # # # # # # # # # # # # # # # Until the end of the line # The code will be ignored by the interpreter.
Also, to facilitate the writing of large chunks of annotations, the Ruby interpreter omits everything in the middle of the line starting with "=begin" and "=end".
#!/usr/bin/env Ruby
=begin
**********************************************************************
This is a comment blocks, something you write for the benefit of
Human readers (including yourself). The interpreter ignores it.
There is no need for a ' # ' in the ' start of every line.
**********************************************************************
=end
Organize your code
Ruby deals with what it reads. There is no compilation process; If there is anything yet to be read, it is simply not defined.
# This results in ' undefined method ' ERROR:
Print successor (3), "\ n"
def successor (X)
X + 1
End
This is not to force you to organize your code from top to bottom, as it was initially thought. As long as you make sure that it is defined before the call, it can safely accept a temporarily undefined reference when the interpreter encounters a method definition.
# Conversion of Fahrenheit to Celsius, broken
# down into two steps.
Def F_to_c (f)
Scale (f-32.0) # This is a forward reference, but it ' s okay.
End
def scale (x)
X * 5.0/9.0
End
printf "%.1f is a comfortable temperature.\n", F_to_c (72.3)
So, on the one hand, it looks a little less convenient than using Perl or Java, but it's not as strict as C (which requires you to always maintain the sort of part you refer to). It is always possible to place the highest level of code at the end of the source file. Even this is much better than seeing it. A wise and painless way to do this is to define main at the top of the file and call it at the bottom.
#!/usr/bin/env Ruby
def main
# Express The top level logic ...
End
# support code here, organized as for you ...
Main # ... and start execution here.
Ruby also provides a chunk of software that divides complex programs into readable, reusable, logically related chunks. We have seen using include to access the module. You will find that load and require are also useful. Load functions like copy of files plus paste (and C's # Include processor directive similar). Require is more complex and is loaded only when it is needed, and is loaded at most once. There are some other differences between load and require; More information can be found in the Language manual, FAQ.
This is it.
This tutorial should be enough to help you start writing Ruby programs. As the problem progresses, you can delve into the manual. FAQs and library references are also important resources.
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.