在 Ruby 中使用 begin/end 是否可以像在 C# 中使用 #region 一样?

发布于 2024-09-09 07:22:42 字数 339 浏览 6 评论 0原文

我最近从 C# 转向了 Ruby,我发现自己失去了创建可折叠、带标签的代码区域的能力。我只是突然想到,做这种事情应该没问题:

class Example
  begin # a group of methods

    def method1
      ..
    end

    def method2
      ..
    end

  end

  def method3
    ..
  end
end

……但是这样做真的可以吗? method1 和 method2 最终与 method3 是同一种东西吗?或者是否有一些我还没有见过的 Ruby 习惯用法可以做到这一点?

I've recently moved from C# to Ruby, and I find myself missing the ability to make collapsible, labelled regions of code. It just occurred to me that it ought to be OK to do this sort of thing:

class Example
  begin # a group of methods

    def method1
      ..
    end

    def method2
      ..
    end

  end

  def method3
    ..
  end
end

...but is it actually OK to do that? Do method1 and method2 end up being the same kind of thing as method3? Or is there some Ruby idiom for doing this that I haven't seen yet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温馨耳语 2024-09-16 07:22:42

正如其他人所说,这不会改变方法定义。

但是,如果您想标记方法组,为什么不使用 Ruby 语义来标记它们呢?您可以使用模块将代码拆分为属于在一起的块。对于大型类来说,这是一个很好的做法,即使您不重用这些模块。当代码增长时,可以轻松地将模块拆分为单独的文件,以便更好地组织。

class Example
  module ExampleGroup
    def method1
      # ..
    end

    def method2
      # ..
    end
  end
  include ExampleGroup

  def method3
    # ..
  end
end

As others have said this doesn't change the method definition.

However, if you want to label groups of methods, why not use Ruby semantics to label them? You could use modules to split your code into chunks that belong together. This is good practice for large classes, even if you don't reuse the modules. When the code grows, it will be easy to split the modules into separate files for better organisation.

class Example
  module ExampleGroup
    def method1
      # ..
    end

    def method2
      # ..
    end
  end
  include ExampleGroup

  def method3
    # ..
  end
end
寄人书 2024-09-16 07:22:42

添加任意 Ruby 代码来取悦您的编辑对我来说似乎根本不合理。我认为你应该尝试获得一个像样的文本编辑器,它能够配置为使用你在注释中放入的某些关键字来启用代码折叠,例如:

# region start AAA

some(:code, :here)

# region end AAA

如果你允许我讽刺:你应该知道Ruby 比 C# 更具表现力、更优雅、更漂亮,而且很可能一开始就不应该隐藏任何 Ruby 代码! ;)

Adding arbitrary Ruby code in order to please your editor doesn't seem reasonable to me at all. I think you should try to get a decent text editor, one that would be able to configure to use some keyword you put in your comments to enable code folding there, for example:

# region start AAA

some(:code, :here)

# region end AAA

And if you allow me to be sarcastic: you should know that Ruby is lot more expressive, elegant and pretty than C#, and chances are that there isn't any Ruby code ought to be hidden in the first place! ;)

喜爱皱眉﹌ 2024-09-16 07:22:42

我知道这可能有点旧,但我遇到了这个问题并找到了一个杀手级的解决方案。如果您使用“Rubymine”,您可以执行#regions。

这是他们谈论区域导航的地方
https://www.jetbrains.com/ruby/help/ navigating-to-custom-region.html

#region Description 
Your code goes here... 
#endregion

因此,在代码中使用上述内容,您将看到以下内容:

Rubymine 代码编辑器 - 使用使用区域未折叠的示例方法
Rubymine 代码编辑器 - 带有使用区域 **un-folded** 的示例方法。

Rubymine 代码编辑器 - 带有示例方法使用区域折叠
Rubymine 代码编辑器 - 带有使用 **regions** 折叠的示例方法。

I know this might be a bit old but I ran into this issue and found a killer solution. If you're using "Rubymine" you can do #regions.

This is where they talk about navigating regions
https://www.jetbrains.com/ruby/help/navigating-to-custom-region.html

#region Description 
Your code goes here... 
#endregion

So with the above in your code you will see the below:

Rubymine code editor - With a sample method using regions un-folded
Rubymine code editor - With a sample method using regions **un-folded**.

Rubymine code editor - With a sample method using regions folded
Rubymine code editor - With a sample method using **regions** folded.

红焚 2024-09-16 07:22:42

我想应该没问题。不过我还没见过很多。另一种可以完成同样事情的方法,同时使用较少的缩进,是关闭并重新打开类,如下所示:

class Example
  def method1
    ..
  end

  def method2
    ..
  end
end

# optionally put some other code here

class Example
  def method3
    ..
  end
end

我认为这经常被使用,以便您可以将其他代码放在中间,例如当在第一个 class Example 块,用于在中间代码中定义常量,中间代码用于在第二个 class Example 块中定义常量。这样你就不必在定义常量之前引用它。

I think it should be OK. I haven't seen it a lot though. Another way you could accomplish the same thing, while using less indentation, is to close and reopen the class, like so:

class Example
  def method1
    ..
  end

  def method2
    ..
  end
end

# optionally put some other code here

class Example
  def method3
    ..
  end
end

I think this is often used so that you can put other code in the middle, such as when there's a constant defined in the first class Example block, which is used to define a constant in the middle code, which is used to define a constant in the second class Example block. That way you never have to refer to a constant before it's defined.

一抹微笑 2024-09-16 07:22:42

是的,这样做是可以的(即它不会改变代码的语义)。

然而,这不是常见的模式,并且可能会让代码的读者感到困惑,因为他们不理解 begin ... end 块的用途。

Yes, it is okay to do that (i.e. it doesn't change the semantics of your code).

However this isn't a common pattern and it might be confusing to readers of your code who wouldn't understand what the purpose of the begin ... end block is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文