rails对于字符串是否与`parameterize`相反?

时间:2021-02-06 18:49:51

I used parameterize method. I want to de-parameterize it. Is there a method to do the opposite of parameterize?

我使用参数化方法。我想取消参数化。有没有方法与参数化相反?

2 个解决方案

#1


14  

No, there is not. parameterize is a lossy conversion, you can't convert it back.

不,那里没有。 parameterize是一个有损转换,你无法将其转换回来。

Here's an example. When you convert

这是一个例子。当你转换

My Awesome Pizza

into

my-awesome-pizza

you have no idea if the original string was

你不知道原来的字符串是不是

My Awesome Pizza
MY AWESOME PIZZA

etc. This is a simple example. However, as you can see from the source code, certain characters are stripped or converted into a separator (e.g. commas) and you will not be able to recover them.

这是一个简单的例子。但是,正如您从源代码中看到的那样,某些字符被剥离或转换为分隔符(例如逗号),您将无法恢复它们。

If you just want an approximate conversion, then simply convert the dashes into spaces, trim multiple spaces and apply an appropriate case conversion.

如果您只想进行近似转换,则只需将破折号转换为空格,修剪多个空格并应用适当的大小写转换。

#2


6  

I'm with Simone on this one but you can always go with

我和Simone在一起,但你可以随时随地

def deparametrize(str)
  str.split("-").join(" ").humanize
end

:)

:)

#1


14  

No, there is not. parameterize is a lossy conversion, you can't convert it back.

不,那里没有。 parameterize是一个有损转换,你无法将其转换回来。

Here's an example. When you convert

这是一个例子。当你转换

My Awesome Pizza

into

my-awesome-pizza

you have no idea if the original string was

你不知道原来的字符串是不是

My Awesome Pizza
MY AWESOME PIZZA

etc. This is a simple example. However, as you can see from the source code, certain characters are stripped or converted into a separator (e.g. commas) and you will not be able to recover them.

这是一个简单的例子。但是,正如您从源代码中看到的那样,某些字符被剥离或转换为分隔符(例如逗号),您将无法恢复它们。

If you just want an approximate conversion, then simply convert the dashes into spaces, trim multiple spaces and apply an appropriate case conversion.

如果您只想进行近似转换,则只需将破折号转换为空格,修剪多个空格并应用适当的大小写转换。

#2


6  

I'm with Simone on this one but you can always go with

我和Simone在一起,但你可以随时随地

def deparametrize(str)
  str.split("-").join(" ").humanize
end

:)

:)