Ruby 的 `end` 分隔符是多余的吗? [英] Is Ruby's `end` delimiter redundant?

查看:33
本文介绍了Ruby 的 `end` 分隔符是多余的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要在 Ruby 中编写 end 分隔符?不能像在 Python 或 CoffeeScript 中那样从缩进中解释吗?

Why do you need to write the end delimiter in Ruby? Can it not be interpreted from the indentation, like in Python or CoffeeScript?

class String
  COLORS.each do |color,code|
    define_method "in_#{color}" do
      "<span style=\"color: ##{code}\">#{self}</span>"
    end
  end
end

对我来说会更好

class String
  COLORS.each do |color,code|
    define_method "in_#{color}" do
      "<span style=\"color: ##{code}\">#{self}</span>"

如果没有,将来是否有可能或是否有任何解决方法?有没有什么语言可以编译成 Ruby(比如 CoffeeScript 编译成 JS)?

If not, will it be possible in the future or are there any workarounds? Is there any language that compiles into Ruby (like CoffeeScript compiles into JS)?

推荐答案

在 Ruby 中空格并不重要.因此,需要清楚地标记代码块的开始和结束位置.

In Ruby spaces are not significant. So, one needs to clearly mark where block of code starts and ends.

如果这对您来说是句法开销,那么您应该看看 Python :)(我不喜欢它正是因为这个原因:重要的空格)

If this looks like a syntactic overhead to you, then you should look at Python :) (I don't like it for exactly this reason: significant spaces)

这篇关于Ruby 的 `end` 分隔符是多余的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆