Skip to content
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

Merged
merged 4 commits into from
Mar 30, 2024

Conversation

khoaxuantu
Copy link
Contributor

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:

  • This PR represents the translation of a single, complete document, or contains only bug fixes.
  • The translation accurately conveys the original meaning and intent of the Chinese version. If deviations exist, I have provided explanatory comments to clarify the reasons.

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:

  • I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.

arr_linked_list

- array.rb
- linked_list.rb
- list.rb
- my_list.rb
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end

# 随机访问元素
Copy link
Owner

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?

Copy link
Contributor Author

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 🤔

Suggested change
# 随机访问元素
=begin 随机访问元素 =end

or

Suggested change
# 随机访问元素
=begin
随机访问元素
=end

Copy link
Owner

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?

Copy link
Contributor Author

@khoaxuantu khoaxuantu Mar 26, 2024

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.

Copy link
Owner

@krahets krahets Mar 26, 2024

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

Copy link
Contributor Author

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great conclusion!

Copy link
Owner

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.

@krahets krahets added the code Code-related label Mar 27, 2024
=end

module Utils
class Printer
Copy link
Owner

@krahets krahets Mar 27, 2024

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.

Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Owner

@krahets krahets left a 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

@krahets krahets merged commit 85ca4cc into krahets:main Mar 30, 2024
1 check passed
@khoaxuantu khoaxuantu deleted the ruby branch March 30, 2024 11:34

```ruby title="list.rb"
# 访问元素
num = nums[1]
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@krahets
Copy link
Owner

krahets commented Mar 30, 2024

@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 en/docs English version. I'll handle them after completing the Ruby translation.

@khoaxuantu
Copy link
Contributor Author

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 en/docs English version. I'll handle them after completing the Ruby translation.

Got it! I have opened a new pull request at #1202 to fix the inconsistent comments. Please help me to review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants