脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Ruby - Ruby中的字符串编写示例

Ruby中的字符串编写示例

2020-05-05 11:31脚本之家 Ruby

这篇文章主要介绍了Ruby中的字符串编写示例,作者给出了相关编程风格的一些建议,需要的朋友可以参考下

优先使用 字符串插值 来代替 字符串串联。

?
1
2
3
4
5
6
7
8
# bad
email_with_name = user.name + ' <' + user.email + '>'
 
# good
email_with_name = "#{user.name} <#{user.email}>"
 
# good
email_with_name = format('%s <%s>', user.name, user.email)

    Consider padding string interpolation code with space. It more clearly sets the
    code apart from the string.考虑使用空格填充字符串插值。它更明确了除字符串的插值来源。

?
1
"#{ user.last_name }, #{ user.first_name }"

    Consider padding string interpolation code with space. It more clearly sets the
    code apart from the string.
    考虑替字符串插值留白。這使插值在字符串里看起來更清楚。

?
1
"#{ user.last_name }, #{ user.first_name }"

    采用一致的字符串字面量引用风格。这里有在社区里面受欢迎的两种风格,它们都被认为非常好 -
    默认使用单引号(选项 A)以及双引号风格(选项 B)。

        (Option A) 当你不需要字符串插值或者例如 \t, \n, ' 这样的特殊符号的
        时候优先使用单引号引用。

?
1
2
3
4
5
# bad
name = "Bozhidar"
 
# good
name = 'Bozhidar'

 

        (Option B) Prefer double-quotes unless your string literal
        contains " or escape characters you want to suppress.
        除非你的字符串字面量包含 " 或者你需要抑制转义字符(escape characters)
        优先使用双引号引用。

?
1
2
3
4
5
# bad
name = 'Bozhidar'
 
# good
name = "Bozhidar"

    第二种风格可以说在 Ruby 社区更受欢迎些。该指南的字符串字面量,无论如何,
    与第一种风格对齐。

    不要使用 ?x 符号字面量语法。从 Ruby 1.9 开始基本上它是多余的,?x 将会被解释为 x (只包括一个字符的字符串)。

  

?
1
2
3
4
5
# bad
 char = ?c
 
 # good
 char = 'c'

    别忘了使用 {} 来围绕被插入字符串的实例与全局变量。

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Person
  attr_reader :first_name, :last_name
 
  def initialize(first_name, last_name)
   @first_name = first_name
   @last_name = last_name
  end
 
  # bad - valid, but awkward
  def to_s
   "#@first_name #@last_name"
  end
 
  # good
  def to_s
   "#{@first_name} #{@last_name}"
  end
 end
 
 $global = 0
 # bad
 puts "$global = #$global"
 
 # good
 puts "$global = #{$global}"

    在对象插值的时候不要使用 Object#to_s,它将会被自动调用。

?
1
2
3
4
5
# bad
message = "This is the #{result.to_s}."
 
# good
message = "This is the #{result}."

    操作较大的字符串时, 避免使用 String#+ 做为替代使用 String#<<。就地级联字符串块总是比 String#+ 更快,它创建了多个字符串对象。

?
1
2
3
4
5
6
7
# good and also fast
html = ''
html << '<h1>Page title</h1>'
 
paragraphs.each do |paragraph|
 html << "<p>#{paragraph}</p>"
end

    When using heredocs for multi-line strings keep in mind the fact
    that they preserve leading whitespace. It's a good practice to
    employ some margin based on which to trim the excessive whitespace.
    heredocs 中的多行文字会保留前缀空白。因此做好如何缩进的规划。这是一个很好的
    做法,采用一定的边幅在此基础上削减过多的空白。

?
1
2
3
4
5
6
7
code = <<-END.gsub(/^\s+\|/, '')
 |def test
 | some_method
 | other_method
 |end
END
#=> "def test\n some_method\n other_method\nend\n"

 

延伸 · 阅读

精彩推荐
  • Rubyruby实现修改ubuntu下的hosts

    ruby实现修改ubuntu下的hosts

    本文给大家分享的是通过ruby获取github上的hosts文件内容,修改到本地Ubuntu中,十分的实用,具体你懂得,有需要的小伙伴可以参考下。 ...

    脚本之家3882020-05-03
  • RubyRuby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails是Ruby世界中一家独大的Web开发框架,要掌握Rails程序的构建,对其目录结构的了解十分必要,下面就来看一下Ruby on Rails所构建的应用程序基本目录结...

    kevinhua4002020-05-09
  • Ruby对Ruby on Rails进行高效的单元测试的教程

    对Ruby on Rails进行高效的单元测试的教程

    这篇文章主要介绍了在Ruby on Rails中进行高效的单元测试的教程,使用到了Ruby的RSpec和Factory Girl框架,需要的朋友可以参考下 ...

    李冠德3562020-04-26
  • RubyRuby编程中关于中断和返回的用法教程

    Ruby编程中关于中断和返回的用法教程

    这篇文章主要介绍了Ruby编程中关于中断和返回的用法教程,作者用代码举例讲解了其中需要注意的问题,需要的朋友可以参考下 ...

    李哲3142020-04-27
  • RubyRuby中对一元操作符重载实例

    Ruby中对一元操作符重载实例

    这篇文章主要介绍了Ruby中对一元操作符重载实例,实例说明如何对一元操作符进行重载,需要的朋友可以参考下 ...

    junjie1852020-04-14
  • RubyRuby学习笔记之gem 命令详解

    Ruby学习笔记之gem 命令详解

    gem是一种文件组织的包,一般的ruby的很多插件都有由这种各种的包提供。我们来看看gem的用法 ...

    hebedich8662020-04-16
  • Ruby深入讲解Ruby中Block代码块的用法

    深入讲解Ruby中Block代码块的用法

    这篇文章主要介绍了深入讲解Ruby中Block代码块的用法,block是Ruby学习进阶当中的重要知识,需要的朋友可以参考下 ...

    pringwq4582020-04-27
  • RubyWindows下ruby语言安装教程

    Windows下ruby语言安装教程

    这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下 ...

    脚本之家5522020-04-22