This chapter deals with some practical issues.
Statement delimiter
Some languages require certain types of punctuation, which are generally semicolons (;) to end each statement in the program. Ruby adopts the convenient method of sh and csh in the shell. Multiple statements in a line are separated by semicolons, but semicolons are not required at the end of the line; a newline is regarded as a semicolon. If the line ends with a backslash (\), subsequent newlines will be ignored; this allows your single logical line to span several lines.
Comments
Why write comments? Although good code can be documented by itself, the idea of thinking that others can understand and quickly understand it the way you do is wrong. In addition, you will be another person after a few days of leaving; after a while we forget what parts of the program we haven't patched or enhanced, you'll say, I know I wrote this, but what exactly am I writing?
Some experienced programmers will point out fairly correctly that contradictory and expired comments are better than none. Of course, having comments does not mean the readability of the code; if your code is not clear, it may be wormy. When you learn Ruby, you will find yourself needing more comments; then they will be reduced when you can express your ideas through simpler, more elegant, readable code.
Ruby follows some common writing habits and uses a pound sign (#) to indicate the beginning of the comment. Codes following the # number until the end of the line # number will be ignored by the interpreter.
At the same time, to facilitate writing large chunks of comments, the Ruby interpreter omits everything in the middle of lines starting with "=begin" and "=end".
#!/usr/bin/env ruby
=begin
**********************************************************************
This is a comment block, something you write for the benefit of
human readers (including yourself). The interpreter ignores it.
There is no need for a '#' at the start of every line.
**********************************************************************
=end
Organize your code
Ruby handles whatever it reads. There is no compilation processing; if there is nothing that has not been read, it is simply considered undefined.
# this results in an "undefined method" error:
print successor(3),"\n"
def successor(x)
x + 1
end
This is not forcing you to organize your code in a top-down way, as you thought at first. As long as you make sure it will be defined before calling, it can safely accept undefined references 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 seems a little less convenient than using Perl or Java, but it is not as strict as writing C (requiring you to always maintain the partial sorting referred to). It is always feasible to put the highest-level code at the end of the source file. Even this is much better than when you see it. A wise and painless good way 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 here...
end
# ... put support code here, organized as you see fit ...
main # ... and start execution here.
Ruby also provides tools to split complex programs into readable, reusable, and logic-related chunks. We have seen accessing modules with include. You will find load and require are also useful. Load functions similar to copy and paste files (similar to C's #include processor instructions). require is more complex and only loads when needed, and loads at most once. There are other differences between load and require; more information can be found in the language manual, FAQ.
That's it...
This tutorial should be enough to help you start writing Ruby programs. As the problem deepens, you can go deep into the manual. FAQ and library reference are also very important resources.
Good luck and happy programming!
Statement delimiter
Some languages require certain types of punctuation, which are generally semicolons (;) to end each statement in the program. Ruby adopts the convenient method of sh and csh in the shell. Multiple statements in a line are separated by semicolons, but semicolons are not required at the end of the line; a newline is regarded as a semicolon. If the line ends with a backslash (\), subsequent newlines will be ignored; this allows your single logical line to span several lines.
Comments
Why write comments? Although good code can be documented by itself, the idea of thinking that others can understand and quickly understand it the way you do is wrong. In addition, you will be another person after a few days of leaving; after a while we forget what parts of the program we haven't patched or enhanced, you'll say, I know I wrote this, but what exactly am I writing?
Some experienced programmers will point out fairly correctly that contradictory and expired comments are better than none. Of course, having comments does not mean the readability of the code; if your code is not clear, it may be wormy. When you learn Ruby, you will find yourself needing more comments; then they will be reduced when you can express your ideas through simpler, more elegant, readable code.
Ruby follows some common writing habits and uses a pound sign (#) to indicate the beginning of the comment. Codes following the # number until the end of the line # number will be ignored by the interpreter.
At the same time, to facilitate writing large chunks of comments, the Ruby interpreter omits everything in the middle of lines starting with "=begin" and "=end".
#!/usr/bin/env ruby
=begin
**********************************************************************
This is a comment block, something you write for the benefit of
human readers (including yourself). The interpreter ignores it.
There is no need for a '#' at the start of every line.
**********************************************************************
=end
Organize your code
Ruby handles whatever it reads. There is no compilation processing; if there is nothing that has not been read, it is simply considered undefined.
# this results in an "undefined method" error:
print successor(3),"\n"
def successor(x)
x + 1
end
This is not forcing you to organize your code in a top-down way, as you thought at first. As long as you make sure it will be defined before calling, it can safely accept undefined references 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 seems a little less convenient than using Perl or Java, but it is not as strict as writing C (requiring you to always maintain the partial sorting referred to). It is always feasible to put the highest-level code at the end of the source file. Even this is much better than when you see it. A wise and painless good way 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 here...
end
# ... put support code here, organized as you see fit ...
main # ... and start execution here.
Ruby also provides tools to split complex programs into readable, reusable, and logic-related chunks. We have seen accessing modules with include. You will find load and require are also useful. Load functions similar to copy and paste files (similar to C's #include processor instructions). require is more complex and only loads when needed, and loads at most once. There are other differences between load and require; more information can be found in the language manual, FAQ.
That's it...
This tutorial should be enough to help you start writing Ruby programs. As the problem deepens, you can go deep into the manual. FAQ and library reference are also very important resources.
Good luck and happy programming!