ITeye博客

ruby 和 c# 3.0

nacu · 2007-07-02 22:28 · ruby · Markdown

看了C# 3.0 的feature, 瀑布汗了一下,MS 开始抄Ruby了么
有下例为证
c# 3.0

c# 代码

 

  1. 10.Times((int i) =>
  2. {
  3. if (i > 5)
  4. Console.WriteLine(i);
  5. else
  6. Console.WriteLine("less than five");
  7. }
  8. );

ruby:

ruby 代码

 

  1. 10.times {|i| puts i.to_s}

还有

c# 代码

 

  1. var dt = 1.Years().Ago();
  2. var dt2 = 1.Minutes().Ago();
  3. Console.WriteLine("one year age:" + dt.ToShortDateString());
  4. Console.WriteLine("one minute age:" + dt2.ToString());

具体实现如下:

c# 代码

 

  1. public static class NumericExtention
  2. {
  3. public static int Years(this int value)
  4. {
  5. return value * 60 * 60  * 24 *365;
  6. }
  7. public static int Seconds(this int value)
  8. {
  9. return value;
  10. }
  11. public static int Minutes(this int value)
  12. {
  13. return value * 60;
  14. }
  15. public static int Hours(this int value)
  16. {
  17. return value * 60 * 60;
  18. }
  19. public static DateTime Ago(this int value)
  20. {
  21. return DateTime.Now.AddSeconds(-value);
  22. }
  23. public static void Times(this int value, Action<int> action)
  24. {
  25. for (int i = 0; i < value; i++)
  26. {
  27. action(i);
  28. }
  29. }
  30. }

评论(13)

  1. #13 楼 · oldrev · 2007-12-09 22:38

    C#每个版本都能有创新,这是守旧的某岛国社区不能比的。

  2. #12 楼 · mooniscrazy · 2007-12-09 09:55

    如果不是故意挑事,就是智商有问题。

  3. #11 楼 · caoyi1983 · 2007-12-04 13:55

    C#3.0是动态类型了?我刚下了vs2008,还没导出功夫装上看看。

  4. #10 楼 · 狂放不羁 · 2007-11-15 19:31

    技术是向前发展的,要发展就要吸取其他语言优秀的地方。站在一个高的起点去发展。其实抄袭也没什么,只要敢承认自己是抄袭的,同时也是为了语言和技术的进步。

  5. #9 楼 · taowen · 2007-10-29 09:16

    我反而觉得C# 3.0的static extension的实现方式简单有效。那可不是module,只是一个静态方法。经常我们写测试的时候就希望expect.call这样的API不是用Expect类加call静态方法实现的,而是ExpectCall一个静态方法。

  6. #8 楼 · shrpcn · 2007-10-29 02:18

    某些人 喜欢把 "MS抄袭" 放在嘴上. 不是MS是否抄袭XXX 的商业上的成功啊?

  7. #7 楼 · Venus · 2007-09-27 10:17

    ray_linn writes:

    抄ruby? 你这不是坐井观天么, closure难道是ruby发明的啊? 那ruby的闭包是不是抄袭了Lisp、Smalltalk 和Haskell,甚至perl lua?

    然

  8. #6 楼 · cat · 2007-08-17 23:30

    CLR这个平台还是很赞的 他们好像在CLR上面做Dynamic Language Runtime,然后再这个基础上做IronRuby (以前还有IronPython, 不过用的人似乎不多)

  9. #5 楼 · liping · 2007-07-27 08:01

    只能说两点: (1) 微软有好的技术团队,敢于面对非议! (2) .net平台的基础设计的很好( CTS 和CLR)

  10. #4 楼 · ray_linn · 2007-07-11 16:44

    抄ruby? 你这不是坐井观天么, closure难道是ruby发明的啊? 那ruby的闭包是不是抄袭了Lisp、Smalltalk 和Haskell,甚至perl lua?

  11. #3 楼 · microhf · 2007-07-04 07:13

    在这里Readonly等人已经对LINQ进行了评价 http://www.iteye.com/topic/76944

    语言应该是博采众长的

    In terms of the evolution of programming languages, LINQ represents a fusion of a typical OO language (C#) with ideas taken from modern dynamic languages such as Ruby and functional languages such as Lisp. It’s a thoroughly interesting idea and its availability probably will make C# a technical advance on Java, for a time at least. However, whether it actually delivers better (more powerful, more reliable and more maintainable) applications - which is presumably what it's all about at bottom for the companies employing .NET developers - depends on how C# and VB programmers take to such concepts as lambda expressions, expression trees and declarative programming.

    http://www.techworld.com/features/index.cfm?FeatureID=2982

  12. #2 楼 · yananay · 2007-07-03 09:39

    吸取别人的长处是好的,可惜ms的平台就是太乱了,部署起来费劲, 尤其是不同版本之间的部署,或者安装了好多MS的软件的时候冲突,头疼。

  13. #1 楼 · hideto · 2007-07-03 07:34

    抄吧抄吧,先是java后是ruby 反正不用微软的破东西

评论功能维护中,当前仅展示历史评论。