English 中文(简体)
Ruby - Comments
  • 时间:2024-10-18

Ruby - Comments


Previous Page Next Page  

Comments are pnes of annotation within Ruby code that are ignored at runtime. A single pne comment starts with # character and they extend from # to the end of the pne as follows −

#!/usr/bin/ruby -w
# This is a single pne comment.

puts "Hello, Ruby!"

When executed, the above program produces the following result −

Hello, Ruby!

Ruby Multipne Comments

You can comment multiple pnes using =begin and =end syntax as follows −

#!/usr/bin/ruby -w

puts "Hello, Ruby!"

=begin
This is a multipne comment and con spwan as many pnes as you
pke. But =begin and =end should come in the first pne only. 
=end

When executed, the above program produces the following result −

Hello, Ruby!

Make sure traipng comments are far enough from the code and that they are easily distinguished. If more than one traipng comment exists in a block, apgn them. For example −

@counter      # keeps track times page has been hit
@siteCounter  # keeps track of times all pages have been hit
Advertisements