简体   繁体   English

如何使用Ruby 2.5.3从数组中删除字符串?

[英]How to remove a string from an array with Ruby 2.5.3?

I develop on Ruby on Rails 5.2. 我在Ruby on Rails 5.2上进行开发。 With the purpose of managing translations, I wish to allow the user to select a language which is different from his current language. 为了管理翻译,我希望允许用户选择一种与当前语言不同的语言。 The list of configured languages of the application is 该应用程序的已配置语言列表为

 all_languages = I18n.config.available_locales

all_languages is an Array. all_languages是一个数组。 puts all_languages returns: puts all_languages返回:

en fr de it

The user language is defined in the users table. 用户语言在用户表中定义。 A method returns current user's language 一种方法返回当前用户的语言

user_language = current_user.language

user_language is a String. user_language是一个字符串。 puts user_language returns: puts user_language返回:

en

I try to apply the delete(obj) method to the array, but this does not alter the array: 我尝试将delete(obj)方法应用于数组,但这不会更改数组:

all_languages.delete(user_language)

I try to work on arrays only, still it does not alter the languages array: 我尝试仅在数组上工作,但仍然不能更改语言数组:

remove_language = Array.new
remove_language << user_language

puts remove_language returns: puts remove_language返回:

en

puts all_languages - remove_language returns: puts all_languages - remove_language返回:

en fr de it

where the en language should be removed. 应该删除en语言的位置。 I don't understand why it remains in the list! 我不明白为什么它仍保留在列表中!

I18n.config.available_locales returns symbols *. I18n.config.available_locales返回符号*。 And your current_user.language is a string. 您的current_user.language是一个字符串。 "en" is not at all the same thing as :en . "en":en That said, this should work: 也就是说,这应该起作用:

all_languages = I18n.config.available_locales.dup # copy the array
all_languages.delete(:en)
# or, for your case
all_languages.delete(current_user.language.to_sym)


# non-mutating way
all_langs_without_en = I18n.config.available_locales.reject { |loc| loc == :en }

* at least in rails 4.2, where I checked this. *至少在我检查过的rails 4.2中。

Try this 尝试这个

all_languages = ["en","fr","de","it"]
user_language = "en"
all_languages.delete_at(all_languages.index(user_language))

#=> ["fr","de","it"]

Your code doesn't work, because available_locales returns an array of symbols and you are attempting to remove a string . 您的代码无效,因为available_locales返回一个符号数组,并且您正在尝试删除字符串

a = [:en, :fr, :de, :it]
a.delete('en')
#=> nil

a #=> [:en, :fr, :de, :it]

To fix this, you have to turn your string into a symbol. 要解决此问题,您必须将字符串变成符号。 Furthermore, you should avoid delete because it modifies the receiver (modifying available_locales might result in bugs later on). 此外,应避免delete因为它会修改接收方(修改available_locales可能会在以后导致错误)。 You can use Rails' Array#without instead which returns a new array: 您可以改用Rails的Array#without返回新数组:

all_languages = I18n.config.available_locales
#=> [:en, :fr, :de, :it]

user_language = current_user.language.to_sym
#=> :en

all_languages.without(user_language)
#=> [:fr, :de, :it]

This solution should meet your need: 该解决方案应满足您的需求:

l = I18n.config.available_locales
l.pop
I18n.config.available_locales = l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM