In The Book of Ruby, Ruby source code is written like this:
def saysomething puts( "Hello" ) end
Often the code will be annotated with comments. Ruby comments are any text following a hash mark (#
) on a single line. The comments are ignored by the Ruby interpreter. When I want to draw attention to some output that Ruby displays or to a value returned by a piece of code (even if that value is not displayed), I indicate this with this type of comment: # =>
. Occasionally, when I want to draw attention to some input that the user should enter, I use this type of comment: # <=
. Here is an example to illustrate these commenting conventions:
puts("Enter a calculation:" ) # Prompt user this is a simple comment exp = gets().chomp() # <= Enter 2*4 comment shows data to enter puts( eval( exp )) # => 8 comment shows result of evaluation
When a piece of code returns or displays too much data to be shown in a single-line comment, the output may be shown like this:
This is the data returned from method #1 This is the data returned from method #2 This is the data returned from method #3
helloname.rb
When a sample program accompanies the code, the program name is shown in the margin as it is here.
Explanatory notes that provide hints or extra information are shown like this:
This is an explanatory note.
More in-depth explanation of points mentioned in the text may be shown in a box like this: