Ruby vs. Python: Multiline Comments and Strings

Ruby vs. Python: Multiline Comments and Strings
Down Arrow

One of the primary differences between Ruby and Python is that Rubyists believe that there are many right ways to do anything and Pythonistas believe there is only one. Multiline comments and strings are a great example of this difference.

In ruby, multiline comments look like this: 

=begin
Multi
Line
Comment
=end

And true to form, ruby provides several options for multiline strings.

1. Just add return characters in a string

str = 'Multi
Line
String'

2. Use a heredoc

str = <<-EOS
Multi
Line
String
EOS

EOS can be replaced with any string of characters. Whatever you use at the top must match what you use at the bottom.

3. Use the % operator

str = %{
Multi
Line
String
}


Pythonistas believe there should be only one correct way to do anything, so true to form, they have one way of doing all of these things. The triple quotation mark:


'''
Multi
Line
Comment
'''


str = '''
Multi
Line
String
'''

The only difference between multiline comments and multiline strings is that strings get used and comments don’t.

William Jeffries

William Jeffries

Stride Alum

No items found.
green diamond