I'm making a Ruby gem, and I want to put comments in the file. Would this be allowed, or would it mess up the gem? My code:
# test
Gem::Specification.new do |s|
s.name = 'My Gem' # Add a better name
s.version = '0.0.0'
s.summary = "Summary of my gem"
s.description = "More detailed description" # Maybe a tiny bit more detailed?
s.authors = ["Me"]
s.email = '[email protected]' # Please don't email me, as I rarely look at my email
s.files = ["lib/myGem.ruby"] # Change to .rb
s.homepage =
'https://rubygems.org/gems/foobar'
end
# Add s.license
=begin
foo
bar
=end
Thanks in advance.
CodePudding user response:
I'm making a Ruby gem, and I want to put comments in the file. Would this be allowed, or would it mess up the gem?
Like most programming languages, Ruby has comments. In fact, Ruby has two kinds of comments:
- Single-line comments start with the token
#
and end with the end of the line:#!/usr/bin/env ruby # Shebang lines are just interpreted as comments, which is clever. some_code # This is a comment. # So is this. foo.bar. # Comments are allowed in lots of places. baz( # And here. 23, # And here. 42, # And here. :foo ) # Even here. .quux
- Multi-line comments start with the token
=begin
at the beginning of a line and end with the token=end
at the beginning of a line:some_code =begin This is a comment. This is still the same comment. So is this. This is not the end of the comment: =end But this is: =end
If you want to know all the gory details, I recommend reading:
- ISO/IEC 30170:2012 Information technology — Programming languages — Ruby specification, section 8.5 Comments
language/comment_spec.rb
of The Ruby Spec Suite akaruby/spec
- Section 2.1 Lexical structure of The Ruby Programming Language by David Flanagan and Yukihiro 'matz' Matsumoto
- The Code Comments sub-section in the Ruby Syntax section of the Ruby documentation
- The introductory parts of Programming Ruby by Dave Thomas, Andy Hunt, and Chad Fowler
CodePudding user response:
The .gemspec
and Gemfile
are actually just plain old Ruby files and the same syntax rules apply as in any other Ruby code. Unlike other more verbose languages (cough cough Java) Ruby is actually very well suited to writing configuration and you'll more often then not find it used instead of XML, JSON or YAML files.
They just don't have an .rb
extension - as to why then thats probally a question that only the original authors can answer to. Another example of this same phenomenon is Rack's config.ru
.