有一个模块MyModule:
module MyModule
extend ActiveSupport::Concern
def first_method
end
def second_method
end
included do
second_class_method
end
module ClassMethods
def first_class_method
end
def second_class_method
end
end
end
Run Code Online (Sandbox Code Playgroud)
当某个类include是这个模块时,它将有2个方法暴露为实例方法(first_method和second_method)和2个类方法(first_class_method和second_class_method) - 很明显.
有人说,那
included块将在包含模块的类的上下文中执行.
这究竟是什么意思?意思是,什么时候second_class_method执行这个方法()?
GitHub使用诸如的链接直接访问公共存储库中的文件https://raw.github.com/user/repository/branch/filename。使用这样的链接以及CURL或WGET,我可以轻松地在公共存储库中获取文件。
最近,我将在GitHub上拥有的一些存储库移到了BitBucket,因为BitBucket免费为我提供了五个私有存储库,而FREE对我来说总是一个好词。但是碰巧我在公共BitBucket存储库中缺少此功能。
BitBucket有类似的功能吗?
如果是这样,获取文件的URL格式是什么?
让我试着解释一下我需要什么.
当我在我的HTML页面中包含像Bootstrap这样的库并加载此页面时,也会加载库主文件.大多数情况下,库文件需要其他文件.由于我使用的引导作为一个例子,让我们考虑glyphicons-halflings-regular.svg,glyphicons-halflings-regular.ttf,glyphicons-halflings-regular.woff和glyphicons-halflings-regular.woff2,个个引导的一部分.
我正在编写一个Rack中间件来处理来自我的应用程序的请求.此中间件的任务之一是根据某些约定修改请求.例如,在我的应用程序中,Bootstrap将位于此路径中
ENV['HOME']/my_app/web/libraries/bootstrap/
Run Code Online (Sandbox Code Playgroud)
然后它的主要CSS文件
ENV['HOME']/my_app/web/libraries/bootstrap/css/bootstrap.css
Run Code Online (Sandbox Code Playgroud)
并且,在加载时,它将为我上面提到的所有这些文件生成新请求.
我需要的是确定某个文件,例如glyphicons-halflings-regular.svg,另一个文件是必需的,因此我可以使用我的Rack中间件来相应地更改其位置.喜欢:
Requested file:
`glyphicons-halflings-regular.svg`
Requested-by:
http://my_app_domain.com/web/libraries/bootstrap/css/bootstrap.css
Request address:
http://my_domain_app.com/web/libraries/bootstrap/css/../fonts/glyphicons-halflings-regular.svg
Run Code Online (Sandbox Code Playgroud)
我想为每个新请求启动一个线程.然后我只需要考虑主文件的地址作为基地址,并将其添加到该文件生成的每个子请求中.
我很想听听你对此的看法.有没有更好的方法来做同样的建议?
编辑:( spickermann的问题)
不,这不是重点.也许我不清楚,然后我会试着换句话说.
考虑以下结构:
ENV['HOME']/my_app/web/libraries/library1/
| |
| |_ a_css_file.css
| |_ a_jpeg_file.jpg
|
/library2/
|
|_ another_css_file.css
|_ a_jpeg_file.jpg
Run Code Online (Sandbox Code Playgroud)
假设我的中间件收到了一个名为的文件的请求a_jpeg_file.jpg.我们有两个同名的文件,一个在其中library1,另一个在library2.
我来决定投放哪个文件的唯一方法是,如果我可以知道,如果a_jpeg_file.jpg被要求a_css_file.css或通过another_css_file.css.
在第一种情况下,我会将路径添加library1到文件并提供服务ENV['HOME']/my_app/web/libraries/library1/.在另一种情况下,我将路径前置library2并服务ENV['HOME']/my_app/web/libraries/library2/.
在这两种情况下,我都需要知道生成请求的文件a_jpeg_file.jpg.
换句话说,我需要知道哪个库上下文需要某个文件.
运行 bundle install 时出现此错误:
Errno::EPERM: Operation not permitted @ dir_s_rmdir - /home/rbtlong/.bundle/cache/compact_index/rubygems.org.443.29b0360b937aa4d161703e6160654e47/versions
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb:50:in `unlink'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb:50:in `delete'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb:50:in `update'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb:64:in `update'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb:55:in `update_and_parse_checksums!'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/fetcher/compact_index.rb:65:in `available?'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/fetcher/compact_index.rb:15:in `call'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/fetcher/compact_index.rb:15:in `block in compact_index_request'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/fetcher.rb:154:in `use_api'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:331:in `block in api_fetchers'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:331:in `select'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:331:in `api_fetchers'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:336:in `block in remote_specs'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/index.rb:10:in `build'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:335:in `remote_specs'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/source/rubygems.rb:82:in `specs'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/lazy_specification.rb:56:in `__materialize__'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/spec_set.rb:91:in `block in materialize'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/spec_set.rb:88:in `map!'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/spec_set.rb:88:in `materialize'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/definition.rb:140:in `specs'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/definition.rb:129:in `resolve_remotely!'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/installer.rb:195:in `resolve_if_need'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/installer.rb:70:in `run'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/installer.rb:22:in `install'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/cli/install.rb:102:in `run'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/cli.rb:175:in `install'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/rbtlong/.rvm/gems/ruby-2.2.3/gems/bundler-1.12.3/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch' …Run Code Online (Sandbox Code Playgroud) 看一下课上的Ruby文档,Enumerable我发现了一些有趣的内容,我想知道为什么会这样.
在#inject描述中我发现了这些例子:
# Sum some numbers
(5..10).reduce(:+) #=> 45
# Same using a block and inject
(5..10).inject { |sum, n| sum + n } #=> 45
# Multiply some numbers
(5..10).reduce(1, :*) #=> 151200
# Same using a block
(5..10).inject(1) { |product, n| product * n } #=> 151200
Run Code Online (Sandbox Code Playgroud)
注意,当#inject用于乘法时,它接收初始值1.我认为这是必要的,因为否则产品将接收0作为起始值(因为它发生在总和中)并且乘法也将为0.事实上,如果我跑
p (1..5).inject(0) { |prod, n| prod * n }
Run Code Online (Sandbox Code Playgroud)
我有
0
Run Code Online (Sandbox Code Playgroud)
但后来我跑了
p (1..5).inject { |sum, n| sum + n …Run Code Online (Sandbox Code Playgroud) 我正在测试以下问题的可能解决方案.我提出的"开膛"和"开膛机_2"的前两个解决方案运行不正常.我想找出原因.
Disembowel_3是迄今为止我最喜欢的解决方案.但是如果我不明白我的前两个解决方案出了什么问题,我觉得我无权使用disembowel_3.
谁能帮我弄清楚前两个解决方案有什么问题?
# Write a function disemvowel(string), which takes in a string,
# and returns that string with all the vowels removed. Treat "y" as a
# consonant.
def disemvowel(string)
string_array = string.split
vowels = %w[aeiou]
i = 0
while i < string.length
if vowels.include? string[i] == true
string_array[i] = " "
end
i +=1
end
new_string = string_array.join
new_string = new_string.sub(/\s+/,"")
return new_string
end
def disemvowel_2(string)
string_array = string.split('')
string_array.delete('a','e','i','o','u')
return string_array.join('')
end
# This is my favorite …Run Code Online (Sandbox Code Playgroud) 我有以下 ReactJS 组件:
import React from 'react';
import marked from 'marked';
import './articles_list.css';
export default class ArticlesList extends React.Component {
constructor(props) {
super(props);
this.state = {
articles: null
}
}
componentWillMount() {
fetch('/articles_all')
.then(res => res.json())
.then(json => {
this.setState({
articles: json.articles
});
});
}
handleClick(e) {
e.preventDefault();
var elem = e.target;
var file = elem.getAttribute('data-file').split('.')[0];
fetch('/article/'+file, {
headers: {
'Accept': 'text/markdown'
}
})
.then(res => res.text())
.then(txt => marked(txt))
.then(html => document.getElementById('article-text').innerHTML = html)
}
render() {
var …Run Code Online (Sandbox Code Playgroud) 在我的 gem 中,我有以下模块:
module EmeraldComponent
def self.create(full_name)
raise StandardError('Base directory for components is missing.') if base_directory_missing?
raise StandardError('An Emerald Component must have a name.') if full_name.empty?
raise StandardError('An Emerald Component must have a namespace.') if simple_name?(full_name)
write_component(full_name)
true
end
def self.write_component(full_name)
## To be implemented
end
def self.simple_name?(full_name)
vet = full_name.split('.')
vet.length == 1
end
def self.base_directory_missing?
not (File.exist?(EmeraldComponent::BASE_DIRECTORY) && File.directory?(EmeraldComponent::BASE_DIRECTORY))
end
end
Run Code Online (Sandbox Code Playgroud)
在我对该模块的 Rspec 测试中,我有以下这些:
context 'create' do
it 'raises an error if the base directory for components is …Run Code Online (Sandbox Code Playgroud) 我有这个React组件,其主要目标是在将文章存储为MarkDown(.md文件)并通过将其转换为HTML之后,呈现该文章marked。
import React from 'react';
import marked from 'marked';
import './articles.css';
export default class Articles extends React.Component {
constructor(props) {
super(props);
this.state = {
articles: [],
last_article: ""
}
}
componentWillMount() {
fetch('/last_article', {
headers: {
'Accept': 'text/markdown'
}
})
.then(res => res.text())
.then(txt => marked(txt))
.then(html => {
this.setState({
last_article: html
});
});
}
render() {
return (
<div className="articles">
{this.state.last_article}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
后端工作正常,可以componentWillMount获取正确的文本并将其完美转换。但是它呈现为:
我不是React专家,以前从未遇到过这个问题。
有什么建议么?
您将获得一个字符串S.计算字符串S中所有数字的出现次数.
输入:第一行包含字符串S.
输出:计算输入数字中出现的次数1并打印输出.我试图像这样尝试给定的问题:
number=$stdin.gets.chomp
number.split('').map(&:to_i)
number.each do |numbers|
i==0
while numbers===1
i+=1
end
end
puts i
Run Code Online (Sandbox Code Playgroud)
但是,它没有执行.有人可以建议我怎么做?