简体   繁体   中英

Ruby block comment and profile questions

I have written a Ruby version of Erik Demaine's (MIT) docdist8.py. This is available on github as docdist-v3.rb . I faced two weird kind of situations:

1) In the function inner_product there is a block comment:

Inner product between two vectors, where vectors
are repeated as dictionaries of (word, freq) pairs.
Example : inner_product({"and":3, "of":2, "the":5},
                        {"and":4, "in":1, "of":1, "this":2}) = 14.0

If I wrap this with =begin and =end there is no problem, but if I wrap it with triple double-quotes """, I get errors as follows:

./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
    Example : inner_product({"and":3, "of":2, "the":5},
                                         ^
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
    Example : inner_product({"and":3, "of":2, "the":5},
                                                  ^
./docdist-v3.rb:72: syntax error, unexpected kIN, expecting kEND
...                 {"and":4, "in":1, "of":1, "this":2}) = 14.0
                              ^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
...         {"and":4, "in":1, "of":1, "this":2}) = 14.0
                              ^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
..."and":4, "in":1, "of":1, "this":2}) = 14.0
                          ^

Are there rules / allowed entries for """ that are different from =begin and =end?

2) When I run my program with the time command it executes in about 0.3 seconds. However, if I put require 'profile' the time it takes becomes very high in comparison - 30 seconds. Hence I don't get the correct output at all. This doesn't seem to be the case with the original Python version, where it takes only a marginal extra time to profile. How do I get the same profile run in Ruby?

Note: The two files I used to run the Ruby program are t2.bobsey.txt and t3.lewis.txt. They are available at http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm

1) Block comments always have the form:

=begin
Comment
=end

You are actually creating a string that is never used:

"""
Not a comment
"""
# => "\nNot a comment\n"

That's why you get an error when adding another quote and that's why the syntax highlighting renders them as strings.

2) It's slower with profiler but I get identical results:

ruby docdist-v3.rb t2.bobsey.txt t3.lewis.txt
File t2.bobsey.txt:262111 lines,49785 words,3354 distinct words
File t3.lewis.txt:1015474 lines,182355 words,8530 distinct words
The distance between the documents is: 0.574160 (radians)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM