简体   繁体   English

用单引号字符串Ruby打印打印变量

[英]Printing printing variable in single quoted string Ruby

I am attempting to send a tweet to twitter using the twitter_oauth gem with the following code: 我正在尝试使用twitter_oauth gem通过以下代码向Twitter发送一条推文:

client.update('.@ #{tweeter}, have a nice day!')

Because of the single quotes I cannot get the variable to display but the tweet will not send if single quote are not used. 由于单引号,我无法显示该变量,但如果不使用单引号,则不会发送推文。 Does anyone have any suggestions as to how to get this to work? 有人对如何使它起作用有任何建议吗? thanks 谢谢

Just replace the ' with " , single quoted strings don't do variable substitution and the other neat things of double quoted strings. They exist because of those missing features they are faster to parse. 只需将'替换为" ,单引号字符串就不会进行变量替换,而双引号字符串还有其他巧妙的事情,它们之所以存在是因为它们缺少一些可以更快解析的功能。

If the tweet doesn't work despite using " then the problem is likely that the variable tweeter contains characters that are not allowed or in some other way invalid (maybe requiring some sort of escaping, eg URL or XML escaping). 如果使用"后tweet仍然不起作用,则问题可能是变量tweeter包含了不允许的字符或以其他某种方式无效的字符(可能需要某种转义,例如URL或XML转义)。

Have you tried the old, java-esque way: 您是否尝试过Java式的旧方法:

client.update('.@ ' + tweeter + ', have a nice day!')

Or using a temporary variable: 或使用临时变量:

message = ".@ #{tweeter}, have a nice day!"
client.update(message)

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

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