In which folder should I put “global” shared partial templates? [duplicate]

The standard is placing all shared partials in app/views/shared, and referencing them as render :partial => ‘shared/partial_name’ If you have a standard “row in a list” partial (say, for an index page), you could use a shared partial like: # To render a single object row: render :partial => ‘shared/item’, :locals => { :item => … Read more

How to reuse beforeEach/afterEach in Jasmine JS?

I think this is partially examined in this blog post and also answered here but i’m adding an adapted answer for your example: Reusable code: function sharedSetup(startPage) { beforeEach(function() { login_as_admin(); browser().navigateTo(startPage); }); afterEach(function() { logout(); }); }; How to use it: describe(‘Services Page’, function() { sharedSetup(‘/services’); it(‘Some test for services page’, function() {}); }); … Read more

How to reduce code duplication when dealing with recursive sum types

Congratulations, you just rediscovered anamorphisms! Here’s your code, rephrased so that it works with the recursion-schemes package. Alas, it’s not shorter, since we need some boilerplate to make the machinery work. (There might be some automagic way to avoid the boilerplate, e.g. using generics. I simply do not know.) Below, your recurseAfter is replaced with … Read more

How much duplicated code do you tolerate? [closed]

Refactoring: Improving the Design of Existing Code The Rule of Three The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor. Three strikes and you refactor. Coders … Read more

DRY Ruby Initialization with Hash Argument

You don’t need the constant, but I don’t think you can eliminate symbol-to-string: class Example attr_reader :name, :age def initialize args args.each do |k,v| instance_variable_set(“@#{k}”, v) unless v.nil? end end end #=> nil e1 = Example.new :name => ‘foo’, :age => 33 #=> #<Example:0x3f9a1c @name=”foo”, @age=33> e2 = Example.new :name => ‘bar’ #=> #<Example:0x3eb15c @name=”bar”> … Read more

How to avoid code duplication implementing const and non-const iterators?

[The best answer was, unfortunately, deleted by a moderator because it was a link-only answer. I understand why link-only answers are discouraged; deleting it, however, has robbed future seekers of very useful information. The link has remained stable for more than seven years and continues to work at the time of this writing.] I strongly … Read more

sed command in dry run

Remove the -i and pipe it to less to paginate though the results. Alternatively, you can redirect the whole thing to one large file by removing the -i and appending > dryrun.out I should note that this script of yours will fail miserably with files that contain spaces in their name or other nefarious characters … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)