Ruby:比Java / C#更灵活?

时间:2023-01-01 17:20:20

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?

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

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...

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

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.

编辑:我知道C#,Java和Ruby是强类型的,只有Ruby(像Python和其他)是动态类型的(而Java / C#是静态类型的)。一些答案说动态类型语言更灵活。这是否必然,它如何影响语法?我只是询问语法灵活性。

(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...)

(PHP也是动态类型的,就我所见,它似乎没有Java / C#更灵活。再说一遍,我的意思是语法方面,不是在部署方面,也不是任何其他方面......)

6 个解决方案

#1


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

我不知道Java或C#,但我让你可以重新定义数字+数字的工作方式。

#2


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

至于我,Ruby中最常用的功能和Java中缺少的是代码块/ lambda / 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 :).

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

#3


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.

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

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.

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

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.

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

In response to PHP being dynamically typed:

响应PHP动态输入:

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).

PHP的类型系统是灵活的,允许类型根据它们使用的上下文自动转换。但是,这实际上并不构成真正的动态类型。语言本身大多是静态的,不允许您在运行时向对象添加属性,例如(至少,我上次检查时)。

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.

Python,很可能是Ruby,实际上是强类型的,这意味着你可以自信地进行类型比较,并且不能像添加数字字符串来获取数字那样的PHP技巧。真正的动态语言通常也允许进行元分类,您可以在运行时调整实例或类的类型,或者将属性添加到其中。

#4


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.

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

#5


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

我评论了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

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.

需要注意的第一点是,fixed_point方法处理逐步将进程应用于某些数据的一般想法,直到它通过某个测试。 sqrt函数定义了查找平方根的过程以及确定何时满足的测试。然后,这些“过程”就像任何其他形式的数据一样被传递,这样fixed_point就可以发挥它的神奇作用。

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

而不是临时存储过程和测试整个事情可能是匿名的。我们可以重写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

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.

如果没有这种能力,我必须将过程和测试定义为单独的函数,并创建一个特殊的sqrt_fixed_point函数来调用它们。据我所知,Java可以使用Functors做类似的事情,但我不知道评论。我在博客或类似网站上看到的共识是Java使得这个非常复杂,以至于你只会尝试它。

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 :-)

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

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

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

#6


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.

所有动态类型语言(如Ruby)通常比静态类型语言(如Java)更灵活。您不必使用类型系统,您通常最终会使用静态类型语言。

#1


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

我不知道Java或C#,但我让你可以重新定义数字+数字的工作方式。

#2


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

至于我,Ruby中最常用的功能和Java中缺少的是代码块/ lambda / 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 :).

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

#3


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.

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

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.

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

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.

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

In response to PHP being dynamically typed:

响应PHP动态输入:

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).

PHP的类型系统是灵活的,允许类型根据它们使用的上下文自动转换。但是,这实际上并不构成真正的动态类型。语言本身大多是静态的,不允许您在运行时向对象添加属性,例如(至少,我上次检查时)。

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.

Python,很可能是Ruby,实际上是强类型的,这意味着你可以自信地进行类型比较,并且不能像添加数字字符串来获取数字那样的PHP技巧。真正的动态语言通常也允许进行元分类,您可以在运行时调整实例或类的类型,或者将属性添加到其中。

#4


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.

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

#5


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

我评论了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

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.

需要注意的第一点是,fixed_point方法处理逐步将进程应用于某些数据的一般想法,直到它通过某个测试。 sqrt函数定义了查找平方根的过程以及确定何时满足的测试。然后,这些“过程”就像任何其他形式的数据一样被传递,这样fixed_point就可以发挥它的神奇作用。

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

而不是临时存储过程和测试整个事情可能是匿名的。我们可以重写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

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.

如果没有这种能力,我必须将过程和测试定义为单独的函数,并创建一个特殊的sqrt_fixed_point函数来调用它们。据我所知,Java可以使用Functors做类似的事情,但我不知道评论。我在博客或类似网站上看到的共识是Java使得这个非常复杂,以至于你只会尝试它。

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 :-)

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

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

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

#6


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.

所有动态类型语言(如Ruby)通常比静态类型语言(如Java)更灵活。您不必使用类型系统,您通常最终会使用静态类型语言。