Ruby:比 Java/C# 更灵活?

发布于 2024-07-10 16:13:48 字数 502 浏览 6 评论 0原文

是因为我是学习 Ruby 的新手,还是它真的比 Java/C# 有更多的方法来编写(相同的)东西? 另外,如果 Ruby 比 Java 更灵活,那么是否有任何通常使用的 Ruby 语言特性以避免混淆?

示例可能是并行分配以及编写字符串的所有不同方式?

注意:我并不是要求与 Java/C# 进行比较...只是这个语言问题...

编辑:我了解 C#、Java 和 Ruby是强类型的,并且只有 Ruby(如 Python 和其他语言)是动态类型的(而 Java/C# 是静态类型的)。 一些答案说动态类型语言更灵活。 这一定是真的吗?它如何影响语法? 我只是询问语法灵活性。

(PHP 也是动态类型的,据我所知,它并不看起来比 Java/C# 更灵活。再说一次,我的意思是在语法方面,而不是在部署方面或任何其他方面......)

Is is that I'm a newbie learning Ruby, or does it really have more ways to write (the same) things than Java/C#? Also, if it is more flexible than Java, are there any linguistic features of Ruby that are generally not used to avoid confusion?

Examples might be parallel assignment and all the different ways to write Strings, perhaps?

Note: I'm not asking for a comparison with Java/C#... just this language question, please...

Edit: I understand that C#, Java and Ruby are strongly typed, and that only Ruby (like Python and others) is dynamically typed (while Java/C# are statically typed). Some of the answers say that dynamically-typed languages are more flexible. Is this necessarily true, and how does it affect syntax? I am only asking about syntactic flexibility.

(PHP is also dynamically typed and it does not seem more flexible than Java/C#, as far as I've seen. Again, I mean in terms of syntax, not in terms of deployment nor any other aspect...)

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

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

发布评论

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

评论(6

抱着落日 2024-07-17 16:13:48

对我来说,Ruby 中最常用而 Java 中缺少的功能是代码块/lambda/闭包。

另一个很棒(但可能很危险)的功能是开放类 - 您可以更改您想要的任何类 - 添加新方法,更改旧方法等。您甚至可以将方法添加到特定对象,而不是整个类:)。

As for me the most used features in Ruby and missing in Java are code blocks/lambdas/closures.

Another great (but maybe dangerous) feature is open classes - you can change whatever class you want - add new method, change old, etc. You can even add method to specific object, not the whole class :).

谁对谁错谁最难过 2024-07-17 16:13:48

另一种与 Ruby 非常相似的动态语言是 Python。 然而,在 Zen of Python 中,其中一条规则规定“做某事应该有一种而且只有一种方式”。 这与 Ruby 截然相反,Ruby 允许如此多的元编程,以至于本质上有无数种方法可以做同样的事情。

也就是说,在 Python 3.0(又名:3000)之前,字符串和 unicode 值是不同的类型,这有点讽刺。 虽然这是有道理的,但人们有时会遇到在两者之间进行大量转换以执行文本操作的问题。

如果您有选择,我几乎建议使用 Python 作为动态语言的入门。 Ruby 没有任何问题,但您可能会发现很少会遇到“正确”的做事方法并不完全明显的情况。

响应 PHP 的动态类型:

PHP 的类型系统非常灵活,允许类型根据使用的上下文自动转换。然而,这实际上并不能产生真正的动态类型。 例如,该语言本身大部分是静态的,不允许您在运行时向对象添加属性(至少我上次检查时是这样)。

Python(很可能还有 Ruby)实际上是强类型的,这意味着您可以自信地进行类型比较,而无法执行 PHP 技巧,例如添加数字字符串来获取数字。 真正的动态语言通常还允许元分类,您可以在运行时调整实例或类的类型,或者向其中任何一个添加属性。

Another dynamic language that's fairly similar to Ruby is Python. However, in the Zen of Python, one of the rules dictates that "there should be one, and only one, way of doing something". This is a polar opposite to Ruby which allows so much meta-programming that there are essentially an infinite number of ways to do the same thing.

That said, its somewhat ironic that up until Python 3.0 (aka: 3000) string and unicode values were different types. While it makes sense, people sometimes get into issues where they're converting between the two a lot to perform text operations.

If you have a choice, I'd almost recommend using Python as your introduction to dynamic languages. There's nothing wrong with Ruby, but you may find you'll run into fewer situations where the "right" way to do something isn't totally obvious.

In response to PHP being dynamically typed:

PHP's type system is flexible, allowing types to be automatically converted based on what context they're used in. This doesn't actually make then real dynamic types, however. The language itself is mostly static and won't allow you to add attributes to objects at runtime, for example (at least, the last time I checked).

Python, and quite possibly Ruby, are actually strongly typed, meaning you can confidently do type comparisons, and can't do PHP tricks like adding numeric strings to get a number. True dynamic languages also often allow for meta-classing where you can adjust the type of an instance or class, or add attributes to either, all at runtime.

昨迟人 2024-07-17 16:13:48

Ruby 是一种动态语言。 C# 和 Java 都是具有强类型的静态类型语言。 v4.0 中的 C# 将添加动态功能,但到目前为止,Java 和 C# 与 Ruby 和 Python 等动态语言相比,拥有完全不同且更严格的范例。

Ruby is a dynamic language. C# and Java are both statically typed language with strong typing. C# in v4.0 will add dynamic features but till now, Java and C# have had a completely different and more strict paradigm than dynamic languages such as Ruby and Python.

在你怀里撒娇 2024-07-17 16:13:48

我评论了 rkj 上面关于 lambda 的回答。 此代码演示了您要求的示例;

def abs(n); (n < 0) ? -n : n; end
def square(n); n * n; end
def average(x, y); (x + y) / 2; end

def fixed_point(x, point, process, test)
  return point if test.call(x, point)
  fixed_point(x, process.call(x, point), process, test)
end

def sqrt(n)
  process = lambda {|n,g| average g, (n/g) }
  test = lambda {|n,g| abs(square(g) - n) < 0.001} 
  fixed_point(n, 1.0, process, test)
end

首先要注意的一点是,fixed_point 方法处理的总体思路是逐步对某些数据应用一个过程,直到它通过某个测试。 sqrt 函数定义了求平方根的过程以及确定何时满足要求的测试。 然后,这些“过程”就像任何其他形式的数据一样被传递,以便固定点可以发挥其魔力。

整个过程可以是匿名的,而不是暂时存储过程和测试。 我们可以将 sqrt 重写为;

def sqrt(n)
  fixed_point( n, 1.0, 
      lambda {|n,g| average g, (n/g)},
      lambda {|n,g| abs(square(g) - n) < 0.001} )
end

如果没有这种能力,我就必须将流程和测试定义为单独的函数,并创建一个特殊的 sqrt_fixed_point 函数来调用它们。 据我所知,Java 可以使用函子做类似的事情,但我不知道如何评论。 我在博客或类似文章中看到的共识是,Java 使这变得非常复杂,以至于您尝试一下就会流鼻血。

当然,Ruby 提供的另一个选择是元编程。 我可以重写 sqrt 以便它使用正确的过程和测试重写(动态)fixed_point,但这可能是对该功能的滥用:-)

ps。 发布的 JoelOnSoftware 链接值得重复; http://www.joelonsoftware.com/items/2006/08/01。 html

I commented on rkj's answer above regarding lambda's. This code demonstrates the example you asked for;

def abs(n); (n < 0) ? -n : n; end
def square(n); n * n; end
def average(x, y); (x + y) / 2; end

def fixed_point(x, point, process, test)
  return point if test.call(x, point)
  fixed_point(x, process.call(x, point), process, test)
end

def sqrt(n)
  process = lambda {|n,g| average g, (n/g) }
  test = lambda {|n,g| abs(square(g) - n) < 0.001} 
  fixed_point(n, 1.0, process, test)
end

The first point to notice is that the fixed_point method handles the general idea of progressively applying a process to some data until it passes a certain test. The sqrt function defines the process of finding a square root and the test to determine when we're to be satisfied. These 'procedures' are then passed just like any other form of data so that fixed_point can work it's magic.

Instead of temporarily storing the process and test the whole thing could be anonymous. We could rewrite sqrt as;

def sqrt(n)
  fixed_point( n, 1.0, 
      lambda {|n,g| average g, (n/g)},
      lambda {|n,g| abs(square(g) - n) < 0.001} )
end

Without this ability, I would have to define both the process and the test as individual functions and create a special sqrt_fixed_point function to call them. As far as I'm aware Java can do something similar using Functors but I don't know enough to comment. The consensus I've seen in blogs or similar is that Java makes this so horrendously complicated that you'll get a nosebleed just trying it.

Of course, another option that Ruby gives is metaprogramming. I could rewrite sqrt so that it rewrites (on the fly) fixed_point using the correct process and test, but this is probably an abuse of the feature :-)

ps. The JoelOnSoftware link is posted deserves repeating; http://www.joelonsoftware.com/items/2006/08/01.html

大海や 2024-07-17 16:13:48

所有动态类型语言(如 Ruby)通常都比静态类型语言(如 Java)更灵活。 您不必与类型系统作斗争,而您通常最终会在静态类型语言中这样做。

All dynamically typed languages (like Ruby) usually are more flexible than statically typed ones (like Java). You don't have to fight with the type system, which you often end up doing in statically typed languages.

塔塔猫 2024-07-17 16:13:48

我不懂 Java 或 C#,但我让你可以重新定义 + 在数字上的工作方式这一事实不言而喻。

I don't know Java or C#, but I let the fact that you can redefine how + works on numbers talk for itself.

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