我换了一台新电脑,从一开始就设置了一个完整的开发环境。其中一个变化是尝试使用asdf而不是rvm。不幸的是,当我尝试在我遇到的某个项目上运行rails s或rails c时: Your Ruby version is 2.7.1, but your Gemfile specified 2.6.3 2.6.3确实是Gemfile指定的版本,在.tool-versions文件和.ruby-version中也是如此。 我缺乏理解是因为我没有ruby 2.7.1。 asdf list =>
ruby
2.6.3 ruby -v
ruby 2.6.3p62 (2019-04-16 revis
我在寻找我的数组的计数/长度。匹配aip then count =4匹配bip count =5这些计数可能每次都不同,这些主机的值可能是3到n,所以我尝试从长度调用。
hosts = ["aip1,aip2,aip3,aip4","bip1,bip2,bip3,bip4,bip5"]
if ! hosts.nil?
hosts.each do|d|
if d.match(/^aip/)
name = "a"
else
name = "b"
end
我试过这样,对我不起作用,有没有
我想在另一个名为basics.rb的ruby文件中require一个名为config.rb的文件。我使用Sinatra作为我的web框架。我相信有办法做到这一点,我只是在文档里找不到任何东西。
希望它看起来像这样
post '/' do
require 'config.rb'
// logic
end
我想从两个表达式中获取任何真值,例如2,或者不获取任何值。它可以通过以下方式完成:
if exp1
a = exp1
elsif exp2
a = exp2
end
我试着让它变得简短,并有以下几点:
a = 1 if exp1|| 2 if exp2
但是,在这种情况下,ruby返回1。在ruby中这样做的正确语法是什么?
我有一个ruby函数创建了一个哈希的实例变量,但在javascript中访问它的值时遇到了麻烦。
这是我的控制器:
class TransferController < ApplicationController
def index
require 'json'
#@transfers = Transfer.select("transfer_id,section_id,sum(net) as net").group("transfer_id,section_id").having("sum(n
我使用faye railscast,在视图中有错误:
<%= render @messages %>
和错误:
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
这是什么意思?<%= @messages %>可以工作,但with render不能。
在Ruby语言中,我理解了extend的基本思想。然而,在这段代码中发生了什么?具体地说,extend做了什么?这只是一种将实例方法转换为类方法的便捷方法吗?为什么要这样做,而不是从一开始就指定类方法?
module Rake
include Test::Unit::Assertions
def run_tests # etc.
end
# what does the next line do?
extend self
end
我知道这个问题在其他地方也有涉及,但我对ruby中多行(块?) if else语句的正确语法有点困惑。
举个例子:
if condition then
do something
do somethingelse
do yetanotherthing
done
else
do acompletelyunrelatedthing
done
我知道如果使用多行,then语句是必需的,但是在else之前的done是必要的吗?这似乎会打破if...else的上下文。当我包含这个done时,我会得到:
syntax error, unexpected '\n', exp
我尝试将字符串中每个单词的第二个字母大写。例如,如果字符串是"how are you",它应该返回0,R,0,而我得到的是'nil','nil','nil‘。为什么我得到的是零?我是不是做错了什么?
def capitalize(string)
words = string.split(" ")
index = 0
words.each do|word|
break if index == words.length
p word[index][1].inspect
index += 1
每当我需要ruby或irb格式的文件时,我都会得到这样的错误:
LoadError: no such file to load -- (insert any filename).rb
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):1
from /usr/bin/irb1.9.1:12:in `<main&