在Ruby中,修剪字符串而不创建新字符串的规范方法是什么?

68bkxrlz  于 2022-12-26  发布在  Ruby
关注(0)|答案(9)|浏览(102)

这就是我现在所拥有的--对于它正在做的工作来说,它看起来太冗长了。

@title        = tokens[Title].strip! || tokens[Title] if !tokens[Title].nil?

假设token是一个通过拆分CSV行获得的数组。现在,如果字符串没有被修改,则strip!chomp!et.等函数都返回nil

"abc".strip!    # => nil
" abc ".strip!  # => "abc"

如果它包含额外的前导或尾随空格而不创建副本,Ruby是怎么说的?
如果我想做tokens[Title].chomp!.strip!的话会更难看

4uqofj5v

4uqofj5v1#

我猜你想要的是

@title = tokens[Title]
@title.strip!

如果#strip!方法没有剥离任何内容,则返回nil;如果剥离了变量,则返回变量本身。
根据Ruby标准,后缀为感叹号的方法会在原处更改变量。

**更新:**这是irb的输出,用于演示:

>> @title = "abc"
=> "abc"
>> @title.strip!
=> nil
>> @title
=> "abc"
>> @title = " abc "
=> " abc "
>> @title.strip!
=> "abc"
>> @title
=> "abc"
l7mqbcuq

l7mqbcuq2#

顺便说一句,现在ruby已经支持只带没有"!"。
比较:

p "abc".strip! == " abc ".strip!  # false, because "abc".strip! will return nil
p "abc".strip == " abc ".strip    # true

同样的,strip不可能没有副本。参见string. c中的源代码:

static VALUE
rb_str_strip(VALUE str)
{
    str = rb_str_dup(str);
    rb_str_strip_bang(str);
    return str;
}
  • Ruby1.9.3p0(2011年10月30日)[i386-明w32]*

更新1:正如我现在看到的--它创建于1999年(参见SVN中的rev #372):
Update2:strip!将不会创建重复项-在1.9.x、2.x和 Backbone.js 版本中都是如此。

fwzugrvs

fwzugrvs3#

没有必要同时strip和chomp,因为strip也会删除尾随的回车符--除非你改变了默认的记录分隔符,而这正是你正在咀嚼的。
Olly的答案在Ruby中已经有了规范的方法,不过如果你发现自己经常这样做,你总是可以为它定义一个方法:

def strip_or_self!(str)
  str.strip! || str
end

给予:

@title = strip_or_self!(tokens[Title]) if tokens[Title]

还要记住,如果标记为nil,if语句将阻止@title被赋值,这将导致它保持其先前的值。如果你希望或不介意@title总是被赋值,你可以将检查移到方法中,进一步减少重复:

def strip_or_self!(str)
  str.strip! || str if str
end

作为一种选择,如果你想冒险,你可以在String本身上定义一个方法:

class String
  def strip_or_self!
    strip! || self
  end
end

给出以下之一:

@title = tokens[Title].strip_or_self! if tokens[Title]

@title = tokens[Title] && tokens[Title].strip_or_self!
dm7nw8vv

dm7nw8vv4#

如果使用Ruby on Rails,则会出现挤压

> @title = " abc "
 => " abc " 

> @title.squish
 => "abc"
> @title
 => " abc "

> @title.squish!
 => "abc"
> @title
 => "abc"

如果只使用Ruby,则需要使用strip
这就是陷阱。。在你的情况下,你想使用带没有爆炸!
strip!当然会返回nil,如果没有操作,它仍然会更新变量,所以strip!不能内联使用。如果你想内联使用strip!,你可以使用没有bang!的版本。

**strip!**使用多行方法

> tokens["Title"] = " abc "
 => " abc "
> tokens["Title"].strip!
 => "abc"
> @title = tokens["Title"]
 => "abc"

剥离单线方法......您的答案

> tokens["Title"] = " abc "
 => " abc "
> @title = tokens["Title"].strip if tokens["Title"].present?
 => "abc"
uinbv5nw

uinbv5nw5#

如果你想使用另一种方法后,你需要这样的东西:

( str.strip || str ).split(',')

这样你就可以脱光衣服,之后还能做点什么:)

a0zr77ik

a0zr77ik6#

我认为您的示例是一种明智的方法,尽管您可以将其稍微简化为:

@title = tokens[Title].strip! || tokens[Title] if tokens[Title]

或者你可以把它放在两行:

@title = tokens[Title] || ''
@title.strip!
13z8s7eq

13z8s7eq7#

如果你有ruby 1.9或者activesupport,你可以简单地

@title = tokens[Title].try :tap, &:strip!

这真的很酷,因为它利用了:try:tap方法,在我看来,它们是ruby中最强大的函数式结构。
一个更可爱的形式,将函数作为符号一起传递:

@title = tokens[Title].send :try, :tap, &:strip!
n3h0vuf2

n3h0vuf28#

我道:

> (@title = " abc ").strip!
 => "abc" 
> @title
 => "abc"
jq6vz3qz

jq6vz3qz9#

@title = tokens[Title].strip! || tokens[Title]

我完全有可能不理解这个主题,但这不是你需要的吗?

" success ".strip! || "rescue" #=> "success"
"failure".strip! || "rescue" #=> "rescue"

相关问题