• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby编程中的语法使用风格推荐

Ruby编程中的语法使用风格推荐

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了ruby 语法,ruby 基础语法下载,ruby编程,ruby元编程,ruby编程语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用 :: 引用常量(包括类和模块)和构造器 (比如 Array() 或者 Nokogiri::HTML())。
    永远不要使用 :: 来调用方法。

  # bad
  SomeClass::some_method
  some_object::some_method

  # good
  SomeClass.some_method
  some_object.some_method
  SomeModule::SomeClass::SOME_CONST
  SomeModule::SomeClass()

</div>

    使用括号将def的参数括起来。当方法不接收任何参数的时候忽略括号。

     

# bad
   def some_method()
    # body omitted
   end

   # good
   def some_method
    # body omitted
   end

   # bad
   def some_method_with_arguments arg1, arg2
    # body omitted
   end

   # good
   def some_method_with_arguments(arg1, arg2)
    # body omitted
   end

</div>

    从来不要使用 for, 除非你知道使用它的准确原因。大多数时候迭代器都可以用来替for。for 是由一组 each 实现的 (因此你正间接添加了一级),但是有一个小道道 - for并不包含一个新的 scope (不像 each)并且在它的块中定义的变量在外面也是可以访问的。

 

  arr = [1, 2, 3]

  # bad
  for elem in arr do
   puts elem
  end

  # note that elem is accessible outside of the for loop
  elem #=> 3

  # good
  arr.each { |elem| puts elem }

  # elem is not accessible outside each's block
  elem #=> NameError: undefined local variable or method `elem'

</div>

    在多行的 if/unless 中坚决不要使用 then。

    

# bad
  if some_condition then
   # body omitted
  end

  # good
  if some_condition
   # body omitted
  end

</div>

    在多行的 if/unless 总是把条件放在与 if/unless 的同一行。

 

  # bad
  if
   some_condition
   do_something
   do_something_else
  end

  # good
  if some_condition
   do_something
   do_something_else
  end

</div>

    喜欢三元操作运算(?:)超过if/then/else/end结构。
    它更加普遍而且明显的更加简洁。

  # bad
  result = if some_condition then something else something_else end

  # good
  result = some_condition ? something : something_else

</div>

    使用一个表达式在三元操作运算的每一个分支下面只使用一个表达式。也就是说三元操作符不要被嵌套。在这样的情形中宁可使用 if/else。

  # bad
  some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else

  # good
  if some_condition
   nested_condition ? nested_something : nested_something_else
  else
   something_else
  end

</div>

    不要使用 if x: ... - 它在Ruby 1.9中已经移除。使用三元操作运算代替。

  # bad
  result = if some_condition then something else something_else end

  # good
  result = some_condition ? something : something_else

</div>

    不要使用 if x; ...。使用三元操作运算代替。

    利用 if and case 是表达式这样的事实它们返回一个结果。

  # bad
  if condition
   result = x
  else
   result = y
  end

  # good
  result =
   if condition
    x
   else
    y
   end

</div>

    在 one-line cases 的时候使用 when x then ...。替代的语法when x: xxx已经在Ruby 1.9中移除。

    不要使用when x; ...。查看上面的规则。

    使用 ! 替代 not.

    

# 差 - 因为操作符有优先级,需要用括号。
  x = (not something)

  # good
  x = !something

  避免使用 !!.

  # bad
  x = 'test'
  # obscure nil check
  if !!x
   # body omitted
  end

  x = false
  # double negation is useless on booleans
  !!x # => false

  # good
  x = 'test'
  unless x.nil?
   # body omitted
  end

</div>

    The and and or keywords are banned. It's just not worth
    it. Always use && and || instead.

    and 和 or 这两个关键字被禁止使用了。它名不符实。总是使用 && 和 || 来取代。

   

 # bad
  # boolean expression
  if some_condition and some_other_condition
   do_something
  end

  # control flow
  document.saved? or document.save!

  # good
  # boolean expression
  if some_condition && some_other_condition
   do_something
  end

  # control flow

</div>

    document.saved? || document.save!

    避免多行的 ? :(三元操作符);使用 if/unless 来取代。

    单行主体喜欢使用 if/unless 修饰符。另一个好方法是使用 &&/|| 控制流程。

    

# bad
  if some_condition
   do_something
  end

  # good
  do_something if some_condition

  # another good option
  some_condition && do_something

</div>

    布尔表达式使用&&/||, and/or用于控制流程。(经验Rule:如果你必须使用额外的括号(表达逻辑),那么你正在使用错误的的操作符。)

   

 # boolean expression
  if some_condition && some_other_condition
   do_something
  end

  # control flow
  document.save? or document.save!

</div>

    避免多行?:(三元操作运算),使用 if/unless 替代。

    在单行语句的时候喜爱使用 if/unless 修饰符。另一个好的选择就是使 and/or 来做流程控制。

   

 # bad
  if some_condition
   do_something
  end

  # good
  do_something if some_condition

  # another good option
  some_condition and do_something

</div>

    永远不要使用 unless 和 else 组合。将它们改写成肯定条件。

    

# bad
  unless success?
   puts 'failure'
  else
   puts 'success'
  end

  # good
  if success?
   puts 'success'
  else
   puts 'failure'
  end

</div>

    不用使用括号包含 if/unless/while 的条件。

 

  # bad
  if (x > 10)
   # body omitted
  end

  # good
  if x > 10
   # body omitted
  end

</div>

    在多行 while/until 中不要使用 while/until condition do 。

   

 # bad
  while x > 5 do
   # body omitted
  end

  until x > 5 do
   # body omitted
  end

  # good
  while x > 5
   # body omitted
  end

  until x > 5
   # body omitted
  end

</div>

    当你有单行主体时,尽量使用 while/until 修饰符。

  # bad
  while some_condition
   do_something
  end

  # good
  do_something while some_condition

</div>

    否定条件判断尽量使用 until 而不是 while 。

  

 # bad
  do_something while !some_condition

  # good
  do_something until some_condition

</div>

    循环后条件判断使用 Kernel

您可能想查找下面的文章:

  • ruby声明式语法的实现例子
  • Ruby的语法和语言特性总结
  • Ruby语法笔记
  • Ruby编程中的语法使用风格推荐
  • Ruby中一些基本语法知识点的罗列汇总

相关文章

  • Ruby迭代器的7种技巧分享
  • 浅析Ruby的源代码布局及其编程风格
  • ruby开发的交互式程序例子
  • Ruby中的方法(函数)学习总结
  • Ruby中使用mechanize批量下载校内网相册照片
  • 实例解析Ruby设计模式编程中Strategy策略模式的使用
  • Ruby on Rails下的图像处理入门教程
  • Ruby基础知识之类
  • Ruby实现的最长公共子序列算法
  • Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Ruby字符串、条件、循环、数组、Hash、类基本操作笔记
    • Ruby实现批量对文件增加前缀代码分享
    • 初步了解一下什么是ruby
    • 简要解读Ruby面向对象编程中的作用域
    • 进一步深入Ruby中的类与对象概念
    • ruby 杂项
    • Ruby语法笔记
    • 在阿里云 (aliyun) 服务器上搭建Ruby On Rails环境
    • ruby 一些简单的例子
    • 在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有