Tag:

如何将两个零00作为整数打印?

我正在研究一些应用学院的练习题,我似乎无法打印两个00来进行时间转换。 这是我到目前为止所拥有的: def time_conversion(minutes) hours = minutes/60 if minutes%60 10 minutes = minutes%60 elsif minutes%60 == 0 minutes = 00 end return “#{hours}:#{minutes}” end time_conversion(360)

Ruby:Titleize:我如何忽略较小的单词,如’和’,’the’,’或等

def titleize(string) string.split(” “).map {|word| word.capitalize}.join(” “) end 这标题化每一个单词,但我如何捕获某些我不想大写的单词呢? 即)杰克和吉尔 请不要使用正则表达式。 更新: 我无法使这段代码工作:我得到它打印所有大写的单词数组,但不是没有下面的列表。 words_no_cap = [“and”, “or”, “the”, “over”, “to”, “the”, “a”, “but”] def titleize(string) cap_word = string.split(” “).map {|word| word.capitalize} cap_word.include?(words_no_cap) end

将字符串拆分为指定大小的块,而不会破坏单词

我需要根据特定大小将字符串拆分为块。 我不能打破块之间的单词,所以我需要抓住当添加下一个单词将超过块大小并启动​​下一个单词时(如果块小于指定大小则可以)。 这是我的工作代码,但我想找到一种更优雅的方式来做到这一点。 def split_into_chunks_by_size(chunk_size, string) string_split_into_chunks = [“”] string.split(” “).each do |word| if (string_split_into_chunks[-1].length + 1 + word.length > chunk_size) string_split_into_chunks << word else string_split_into_chunks[-1] << " " + word end end return string_split_into_chunks end

Ruby字符串连接问题

这很有效 irb(main):001:0> name = “Rohit ” “Sharma” => “Rohit Sharma” 但事实并非如此 irb(main):001:0> fname = “Rohit ” => “Rohit ” irb(main):002:0> lname = “Sharma” => “Sharma” irb(main):003:0> name = fname lname 它给出了这个错误 NoMethodError: undefined method `fname’ for main:Object from (irb):3 请提供一些建议。 提前致谢。 UPDATE 得到答案后,我写了一篇博文 。 请检查一下。

ruby字符串分隔符中’%{}’,’%Q {}’,’%q {}’之间的差异

我正在浏览一篇关于ruby的在线教程,并发现了这个“General Delimited Strings”, %{a word} # => “a word” %Q{a word} # => “a word” %q{a word} # equivalent to single quoted version. 所以我在irb上尝试过,这就是我所看到的 2.0.0p247 :025 > %Q(hi) => “hi” 2.0.0p247 :026 > %q(the) => “the” 2.0.0p247 :027 > %q(th”e) => “th\”e” 2.0.0p247 :028 > %q(th’e) => “th’e” 2.0.0p247 :029 > %Q(h’i) => “h’i” 2.0.0p247 […]

Ruby:如何检查字符串是否包含多个项目?

我熟悉Ruby的include? 字符串的方法,但如何检查字符串的多个东西? 具体来说,我需要检查一个字符串是否包含“Fwd:”或“FW:”(并且应该不区分大小写) 示例字符串将是:“FWD:您的Amazon.com订单已发货”

当字符串太长时截断字符串

我有两个字符串: short_string = “hello world” long_string = “this is a very long long long …. string” # suppose more than 10000 chars 我想将print的默认行为更改为: puts short_string # => “hello world” puts long_string # => “this is a very long long…..” long_string仅部分打印。 我试图改变String#to_s ,但它没有用。 有谁知道怎么做这样吗? 更新 实际上我想它运作顺利,这意味着以下情况也可以正常工作: > puts very_long_str > puts [very_long_str] > puts {:a => […]

如何在字符串中使用转义字符

我一直在研究Ruby Koans,并对“escape子句和单引号字符串”示例感到困惑。 一个例子表明我不能以这种方式真正使用转义字符,但紧接着,给出了以下示例: def test_single_quotes_sometimes_interpret_escape_characters string = ‘\\\” assert_equal 2, string.size # <– my answer is correct according to the program assert_equal "\\'", string # <– my answer is correct according to the program end 这使我在两个方面感到困惑: 单引号有时可以与转义字符一起使用。 当assert_equal为”\\\'”时,为什么字符串大小为2 ? (我个人认为答案是”\'” ,这对于尺寸更有意义)。

将字符串截断为前n个单词

将字符串截断为前n个单词的最佳方法是什么?

Ruby:将转义字符串写入YAML

下列… require ‘yaml’ test = “I’m a b&d string” File.open(‘test.yaml’, ‘w’) do |out| out.write(test.to_yaml) end ……输出…… — this is a b&d string 我怎样才能输出它 — ‘this is a b&d string’ ???