-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add ruby code - chapter "array & linked list" #1158
Conversation
- array.rb - linked_list.rb - list.rb - my_list.rb
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) | ||
=end | ||
|
||
# 随机访问元素 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there another comment format to label the headings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there another comment format to label the headings?
Ruby only supports #
for single-line comments. So I think I will place it in a multi-line comment block. For example 🤔
# 随机访问元素 | |
=begin 随机访问元素 =end |
or
# 随机访问元素 | |
=begin | |
随机访问元素 | |
=end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=begin 随机访问元素 =end
I see. Do you think it is a common usage? And what is the industrial standard style of heading comments for Ruby?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=begin 随机访问元素 =end
I see. Do you think it is a common usage? And what is the industrial standard style of heading comments for Ruby?
Actually I barely see this usage in practice. Most of the time when I'm working with Ruby I see people prefer using the second one if they want to write a heading comment
=begin
随机访问元素
=end
It is clear enough to understand that they want to state a heading instead of a normal comment.
But in common, we use multi #
more for heading comment of classes/modules/methods because the =begin...=end
block only works with no indentation. If it is indented like this, then it will raise syntax error.
=begin
随机访问元素
=end
=> SyntaxError
If it is ok for you, I will push another commit to resolve your 2 reviews now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. How about ### 随机访问元素 ###
? A pythonic solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. How about
### 随机访问元素 ###
? A pythonic solution
That's great! We can use it as our own Ruby heading comment convention. With this format, we can even remain compatible with the whole Python sample code.
I would like to briefly summarize the incoming Ruby comment conventions for the project:
- Heading comment:
### Heading ###
- Inline comment:
#
- Multiline comment:
# Abc
# Xyz
# Some thing
- File header:
=begin
File:
Created Time:
Author:
=end
- Classes/modules/methods comment (following Yardoc conventions)
# Generate a linked list from an array
#
# @param [Array<Integer>] arr
#
# @return [ListNode] the root of the linked list
def generate_from_array(arr)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great conclusion!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, we'd like to avoid using the "Classes/modules/methods comment" to reduce the lines of the code.
f028f32
to
986571f
Compare
codes/ruby/utils/print_util.rb
Outdated
=end | ||
|
||
module Utils | ||
class Printer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the class if it is not necessary. (In Java, every method should belong to a class)
I think it'd be good for you to refer to the Python code. It seems like Ruby is more similar to Python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I wrote them based on the Java files, I will update them based on the Python files instead.
But I see that the method linked_list_to_list
in python/modules/list_node.py
is different from the code in other programming languages, which are getListNode
instead. Which one should I implement for the Ruby?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getListNode()
and linked_list_to_list()
are both not called in the source code
I'll remove the getListNode()
for all the languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clean code! Looking forward to your next PR
|
||
```ruby title="list.rb" | ||
# 访问元素 | ||
num = nums[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments is not consistent with Python version
nums << 4 | ||
|
||
# 在中间插入元素 | ||
nums.insert 3, 6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
nums.insert 3, 6 | ||
|
||
# 删除元素 | ||
nums.delete_at 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
@khoaxuantu I've added Ruby code blocks to the document in #1200 and it's more convenient to add the code. And I found inconsistency of comments between Ruby and Python. Please fix them and carefully check the code. Moreover, you don't have to add Ruby code to |
Got it! I have opened a new pull request at #1202 to fix the inconsistent comments. Please help me to review it. |
If this pull request (PR) pertains to Chinese-to-English translation, please confirm that you have read the contribution guidelines and complete the checklist below:
If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist: