需要“String”为:String

时间:2014-03-05 12:27:10

标签: ruby-on-rails ruby string variables

我需要将“string”改为:string

此刻我有这个并且给出了这个:

Holidays.between(from, to, ":de")

但我需要这个

Holidays.between(from, to, :de)

请帮助!

2 个答案:

答案 0 :(得分:4)

String#to_sym是您需要在字符串上调用to_sym并将其变为符号

在将字符串转换为字符串之前,您需要删除字符串中的:(以获得所需的确切符号),因此下面的内容就是这样做

Holidays.between(from, to, ":de".sub(":","").to_sym) #=> Holidays.between(from, to, :de)

答案 1 :(得分:3)

假设您使用的是Holidays gem,只需从字符串中删除冒号,即使用"de"代替":de"。宝石为您处理转换:

require 'holidays'

from = Date.civil(2014, 1, 1)
to = Date.civil(2014, 12, 31)

Holidays.between(from, to, "de")
#=> [{:date=>#<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>, :name=>"Neujahrstag", :regions=>[:de]}, {:date=>#<Date: 2014-04-18 ((2456766j,0s,0n),+0s,2299161j)>, :name=>"Karfreitag", :regions=>[:de]}, {:date=>#<Date: 2014-04-21 ((2456769j,0s,0n),+0s,2299161j)>, :name=>"Ostermontag", :regions=>[:de]}, {:date=>#<Date: 2014-05-01 ((2456779j,0s,0n),+0s,2299161j)>, :name=>"Tag der Arbeit", :regions=>[:de]}, {:date=>#<Date: 2014-05-29 ((2456807j,0s,0n),+0s,2299161j)>, :name=>"Christi Himmelfahrt", :regions=>[:de]}, {:date=>#<Date: 2014-06-09 ((2456818j,0s,0n),+0s,2299161j)>, :name=>"Pfingstmontag", :regions=>[:de]}, {:date=>#<Date: 2014-10-03 ((2456934j,0s,0n),+0s,2299161j)>, :name=>"Tag der Deutschen Einheit", :regions=>[:de]}, {:date=>#<Date: 2014-12-25 ((2457017j,0s,0n),+0s,2299161j)>, :name=>"1. Weihnachtstag", :regions=>[:de]}, {:date=>#<Date: 2014-12-26 ((2457018j,0s,0n),+0s,2299161j)>, :name=>"2. Weihnachtstag", :regions=>[:de]}]

目前还不清楚冒号来自哪里(看起来像一个错误),但你应该把它固定在那里,而不是转换字符串。