手机有很多信息.
电子邮件地址包含许多消息.
消息要么属于电话,要么是电子邮件,要么都不属于.该belongs_to关联是可选的.
以下关联似乎适用于这些关系:
has_many :messageshas_many :messagesbelongs_to :phones, :email这可以,还是有一些正确的方法来指定"can_belong_to"关系?
我对如何在使用Postgres和Heroku的Rails 4.2应用程序中配置数据库感到困惑.
按照Heroku指南中的建议,你会得到config/database.yml这样的:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,我的开发和测试环境使用与登台环境相同的数据库(注意文件没有用于登台的配置).那不好.
这个Heroku指南用于连接到Ruby中的数据库,提到4.2之前的任何Rails应用程序都会database.yml被Heroku覆盖它们的文件.Heroku将解析DATABASE_URL环境变量并创建一个新database.yml文件.
因此,我认为可以database.yml在Heroku上的任何环境中省略配置,例如暂存和生产.你的database.yml文件基本上看起来像Hound(注意缺少生产配置).
development: &default
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
test:
<<: *default
database: app_test
Run Code Online (Sandbox Code Playgroud)
但由于我们使用的是Rails 4.2,我认为Heroku不会覆盖该database.yml文件.在这种情况下,您是否必须为database.ymlHeroku上的环境指定数据库配置?或者将它们排除在外仍然是安全的吗?如果我们确实需要为Heroku环境指定配置,那么以下内容是否足够?
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: …Run Code Online (Sandbox Code Playgroud) 在fish-shell脚本中,如何向stderr打印错误消息?
例如,此消息应该转到stderr流而不是默认的stdout流.
echo "Error: $argv[1] is not a valid option"
Run Code Online (Sandbox Code Playgroud) 给定一个foo.fish只打印“foo”、帮助或错误的鱼脚本
function foo
__parse_args $argv[1]
echo foo
end
function __parse_args --argument option
if test -z $option
return # No option to parse, return early
end
switch $option
case -h --help
echo "Shows this help and exits"
return 0 # How can we exit 0 instead of return?
case -\*
echo "Error: '$option' not a valid option"
return 1 # How can we exit 1 instead of return?
end
end
Run Code Online (Sandbox Code Playgroud)
实际行为:
? foo -h
Shows this help and exits …Run Code Online (Sandbox Code Playgroud) 我们的项目具有以下高级目录结构*
./datascience/
??? core
? ??? setup.py
??? notebooks
? ??? Pipfile
??? web
??? Pipfile
Run Code Online (Sandbox Code Playgroud)
*为简洁起见,排除了所有不相关的文件和目录。
该core软件包是一个库。它是notebooks和web应用程序的依存关系。
该core程序包是一个库,其依赖项在setup.py
import setuptools
setuptools.setup(
install_requires=[
'some-dependency',
'another-dependency'
]
)
Run Code Online (Sandbox Code Playgroud)
在web和notebooks应用程序是使用pipenv依赖管理。它们的依赖关系在中指定Pipfile。
例如,这是在web中指定依赖项的方式web/Pipfile:
[packages]
datascience-core = {path = "./../core"}
flask = "~= 1.0"
Run Code Online (Sandbox Code Playgroud)
注意,core依赖关系是本地依赖关系,因此是相对路径。
做一个pipenv install从内部web或notebooks目录,不安装的依赖性core如我所料库!
我还尝试使用Pipfilefor …
许多标准库容器都具有接受迭代器指定范围的操作.例如,std::list::assign(InputIterator first, InputIterator last).
但是因为许多这些操作不会抛出异常,如果指定的范围[first,last)无效,则会导致未定义的行为.
那么什么是有效范围?我认为它意味着在容器first之前或等于last容器,因为当我尝试其他方式(即first之后last),程序的执行将挂起.
例如:
std::list.assign(container.begin(), container.end()); // valid range
std::list.assign(container.end(), container.begin()); // invalid range
Run Code Online (Sandbox Code Playgroud)
现在假设有效范围是什么,我们如何检查范围是否有效?
我想出了一个辅助函数,它检查在到达容器末尾之前first是否等于last它.如果是真的,这是一个强有力的保证,first因为last范围是有效的.
template <typename Iterator>
bool isValidRange(Iterator first, Iterator last, Iterator end)
{
for (; first != end; ++first)
if (first == last) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
这不是很方便,因为除了指定范围的迭代器之外,还必须将迭代器传递给容器的末尾.
它是否正确?有一个更好的方法吗?
你会如何遍历在一个字符串鱼贝?
不幸的是,遍历字符串并不像遍历列表那样直接:
? for c in a b c
echo $c
end
a
b
c
? for c in abc
echo $c
end
abc
Run Code Online (Sandbox Code Playgroud) 尝试Pipfile为特定的 Python 版本创建一个,但 pipenv 无法检测到已安装的 Python 版本存在。使用asdf安装和管理 Python 版本。
? python --version
Python 3.6.6
? pipenv --python 3.6.6
Warning: Python 3.6.6 was not found on your system…
You can specify specific versions of Python with:
$ pipenv --python path/to/python
? asdf current python
3.6.6 (set by /Users/dennis/some/project/.tool-versions)
Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
类型'char'的无效操作数和未解析的重载函数类型>'到二进制'运算符<<'
这是什么意思?
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("records.txt");
ofstream outFile;
outFile.open("corrected.txt");
while (inFile.good())
{
string num, temp;
inFile >> num;
outFile << temp.at(0)=num.at(9) << temp.at(1)=num.at(8)
<< temp.at(2)=num.at(7) << temp.at(3)=num.at(6)
<< temp.at(4)=num.at(5) << temp.at(5)=num.at(4)
<< temp.at(6)=num.at(3) << temp.at(7)=num.at(2)
<< temp.at(8)=num.at(1) << temp.at(9)=num.at(0) << endl;
// invalid operands of types 'char' and unresolved overloaded function type>'
// to binary 'operator<<'
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序应该反转已反转的电话号码列表.
当使用RestClient图书馆,我可以得到response的GET是这样的:
resource = RestClient::Resource.new some_url
resource.get do |response|
# Handle response
end
Run Code Online (Sandbox Code Playgroud)
我还可以添加request和result参数:
resource.get do |response, request, result|
# Handle response, request, and result
end
Run Code Online (Sandbox Code Playgroud)
Ruby如何返回一个或多个这样的值?
这个问题不是要问的是如何在与模式匹配的每一行的下方或上方添加一个新行.
我要做的是在一行上存在的模式之间添加一个新行.
这是一个例子.
之前:
周一:8 AM-10PM
后:
星期一:
8 AM-10PM
因此,在这种情况下,在每个"星期一"模式后插入新行.
我在Rails 4中有一个表单,我想使用Bootstrap的单选按钮来设置二进制值,而不是使用复选框.
我正在使用Bootstrap的示例作为参考.
我希望它看起来像这样:

<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" name="options" id="option1"> True
</label>
<label class="btn btn-default">
<input type="radio" name="options" id="option2"> False
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/52VtD/3456/
但这是我到目前为止(使用HAML):

.btn-group{"data-toggle" => "buttons"}
%label.btn.btn-default
= f.radio_button :single_use, true
True
%label.btn.btn-default
= f.radio_button :single_use, false
False
Run Code Online (Sandbox Code Playgroud)
哪个产生:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<label class="radio" for="something_single_use_true">
<input id="something_single_use_true" name="something[single_use]" type="radio" value="true">
</label>
Single use
</label>
<label class="btn btn-default">
<label class="radio" for="something_single_use_false">
<input checked="checked" id="something_single_use_false" name="something[single_use]" type="radio" value="false">
</label>
Unlimited use
</label> …Run Code Online (Sandbox Code Playgroud) 我是Javascript的新手,我试图通过将许多功能集成到一起来简化我的生活.
我有一个非常简单的操作,允许单击按钮加载特定图像来代替默认图像.由于反对拥有许多做同样事情的功能,我宁愿只有一个.
我想你可以将图像存储在一个数组中并按位置选择它们?
这是我到目前为止所拥有的.
function swap1() {
document.getElementById("default").src="321.jpg";
}
function swap2() {
document.getElementById("default").src="432.jpg";
}
function swap3() {
document.getElementById("default").src="742.jpg";
}
Run Code Online (Sandbox Code Playgroud)
<input type="button" onClick="swap1()">
<input type="button" onClick="swap2()">
<input type="button" onClick="swap3()">
Run Code Online (Sandbox Code Playgroud)