我经常遇到一堆复杂的if语句,Ruby 的清理方法是什么?
(在这个服务对象示例中,一个 foo 有很多条。这是为了将一个 bar 转移到不同的 foo。)
class BarManager
include FancyErrorLogger
def self.transfer(bar, new_foo)
# Is a move needed? Is this line superfluous and a premature optimisation?
return true if bar.foo_id == new_foo.id
# Checks that bar can be moved to new_foo. Many more elsifs in practice, needs refactoring. These examples demonstrate the potential complexity of each step, preventing the use of overly simplistic solutions such as seen here http://codereview.stackexchange.com/questions/14080/avoiding-a-lot-of-ifs-in-ruby
if bar.dependency == :do_not_move_me or bar.some_condition == …Run Code Online (Sandbox Code Playgroud) 这看起来应该很容易.
我正在创建一个CLI命令gem,因此该命令bin/gem_name没有.rb扩展名.当我尝试在RubyMine中编辑该文件时,它没有给它一个语言模式(对不起,不知道正确的术语)来使RubyMine中的语法高亮和其他漂亮工作.
我怎么能告诉RubyMine这实际上是一个Ruby文件?
我不理解在Ruby中使用Cucumber时的范围,特别是关于实例变量.
对于我的直接示例的上下文,在hooks.rb变量的Before部分中@browser被赋值.
@browser = Watir::Browser.new @browser_selected.to_sym
(@browser_selected通常是'铬')
在步骤定义中,使用@browser.举个简单的例子:@browser.send_keys(:tab)
我不明白的是什么对象包含@browser作为属性.它在这方面有什么意义?我知道我困惑的代码总是在一个块中,并且我认识到每个这样的块被使用(通过它附加的Given/When/Then消息)以某种神秘的方式进行预处理.
这种神秘的遮蔽是实例变量的范围.如何知道这些块中的实例变量的范围?
上下文:TeamCity检测到git提交,并启动自动构建.
鉴于git commit hash和TeamCity配置,如何在包含提交的配置中找到构建,而不必通过Web UI手动检查每个构建?
我正在学习设计模式和它周围的东西(特别是SOLID和Dependency inversion原理),看起来我正在失去一些东西:
遵循DIP规则,我应该能够通过不在类(组合)中创建对象,而是将对象引用/指针发送到类构造函数(聚合)来使类不那么脆弱.但这意味着我必须在其他地方创建一个实例:因此,具有聚合的一个类越灵活,另一个类就越脆弱.
请解释一下我错在哪里.
c++ design-patterns composition aggregation dependency-inversion
我了解当我们在 Spring 安全策略中使用 MODE_THREADLOCAL 和 MODE_INHERITABLETHREADLOCAL 时如何以及会发生什么。我不明白的是,为什么有人会使用 MODE_THREADLOCAL 而不是 MODE_INHERITABLETHREADLOCAL。
我正在尝试学习创建型设计模式,我想我现在了解工厂方法模式。但是在转向抽象工厂模式时,我找不到它的用途。我知道我想念这个,但不知道在哪里。
在抽象工厂模式中,我们将有一个抽象工厂,具体工厂将返回实例。假设我们正在处理汽车的创建。我们将有一个抽象工厂,比如
public interface CarFactory{
public Car getCar();
}
Run Code Online (Sandbox Code Playgroud)
我们的具体工厂将类似于
public class AudiFactory{
public Car getCar(){
return new Audi();
}
}
public class VolvoFactory{
public Car getCar(){
return new Volvo();
}
}
Run Code Online (Sandbox Code Playgroud)
在用户类中,我们将像这样使用它
CarFactory factory = new AudiFactory();
Car carAudi = factory.getCar();
factory = new VolvoFactory();
Car carVolvo = factory.getCar();
Run Code Online (Sandbox Code Playgroud)
我认为我们也可以使用工厂模式构建相同的功能
public class CarFactory{
public Car getCar(String make){
if("Audi".equals(make))
return new Audi();
else if("Volvo".equals(make))
return new Volvo();
}
}
Run Code Online (Sandbox Code Playgroud)
在用户类中我们可以
CarFactory factory = new CarFactory();
Car carAudi = factory.getCar("Audi");
Car …Run Code Online (Sandbox Code Playgroud) 我试过这段代码,但它引发了错误: NameError: uninitialized constant RSpec::Mocks::Mock
RSpec::Mocks::Mock.stub(:i18n_scope).and_return(:activerecord)
model = double(:model, errors: double(:errors, full_messages: []))
ActiveRecord::RecordInvalid.new(model)
Run Code Online (Sandbox Code Playgroud)
我怎么能存根i18n_scope?
我正在使用RSpec(Rails 4.2.5 上的3.4 )测试视图。我在CRUD控制器中使用了decent_exposure gem。decent_exposure使得定义命名方法变得容易,这些命名方法可用于我的视图并记住结果值。
但是我不明白如何在视图规范中添加这些方法。我尝试根据RSpec文档进行操作,但是会引发错误。
为什么不起作用?如何article在视图中存根方法?
我的控制器
class Account::ArticlesController < Account::BaseController
expose(:articles) { current_user.articles }
expose(:article, attributes: :article_params)
# POST /account/articles
# POST /account/articles.json
def create
respond_to do |format|
if article.save
format.html { redirect_to account_article_url(article), notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: account_article_url(article) }
else
format.html { render :new }
format.json { render json: article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /account/articles/1
# …Run Code Online (Sandbox Code Playgroud) 我尝试阅读一些有关重构的教程,但我在条件方面苦苦挣扎。我不想使用三元运算符,但也许应该将其提取为方法?还是有使用地图的聪明方法?
detail.stated = if value[:stated].blank?
nil
elsif value[:stated] == "Incomplete"
nil
elsif value[:is_ratio] == "true"
value[:stated] == "true"
else
apply_currency_increment_for_save(value[:stated])
end
Run Code Online (Sandbox Code Playgroud) ruby ×3
refactoring ×2
rspec ×2
rspec3 ×2
aggregation ×1
c++ ×1
composition ×1
cucumber ×1
git ×1
java ×1
rspec-rails ×1
rubymine ×1
security ×1
stub ×1
teamcity ×1
thread-local ×1