mixins

How to model mixins / multiple interfaces in Haskell?

泪湿孤枕 提交于 2019-12-03 17:31:34
问题 I came across this question on modeling inheritance in Haskell and it reminded me that I have a little more complicated version of the same problem. I'll adopt the example from there because it's easier than thinking up my own. Suppose your program contains a number of types: data Camera = Camera ... data Light = SpotLight ... | DirectionalLight ... data Object = Monster ... | Player ... | NPC ... Now you want to implement some basic physics, so you want them all to have a position and a

How to include a module in a factory_girl factory?

家住魔仙堡 提交于 2019-12-03 17:06:49
问题 I'm trying to reuse a helper method in all my factories, however I cannot get it to work. Here's my setup: Helper module (in spec/support/test_helpers.rb) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = ['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten (0...length).map{ chars[rand(chars.size)] }.join end end end A factory (in spec/factories/users.rb) FactoryGirl.define do factory :user do sequence(:username)

Mixins, variadic templates, and CRTP in C++

╄→尐↘猪︶ㄣ 提交于 2019-12-03 17:02:57
问题 Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4

Breaking ruby module across several files

落爺英雄遲暮 提交于 2019-12-03 15:54:25
问题 I have a ruby module that is supposed to wrap up quite a few classes module A class First #somemethods end class Second #somemethods end class Third #somemethods end end What i would like to do in rails is to break up these classes into several files what might be the best practice to split this huge module into several relevant files? 回答1: One approach would be to come up with directory structure like this: (root dir) ├── a │ ├── first.rb │ ├── second.rb │ └── third.rb └── a.rb Files

“The Ruby way” (mixins and class reopening) vs. dependency injection

人走茶凉 提交于 2019-12-03 15:22:36
问题 In studying mixins vs. dependency injection, I often hear the phrase "the Ruby way." Often developers say something along the lines of Ruby lets you reopen classes and redefine methods means that you can easily "inject" new references into your code at test-time. (see #6 at http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited) But testing is not my main concern; my concern is class reuse. I want classes I can reuse in multiple enterprise-scale Rails applications. So what happened to

Initializing instance variables in Mixins

旧城冷巷雨未停 提交于 2019-12-03 12:15:53
问题 Is there any clean way to initialize instance variables in a Module intended to be used as Mixin? For example, I have the following: module Example def on(...) @handlers ||= {} # do something with @handlers end def all(...) @all_handlers ||= [] # do something with @all_handlers end def unhandled(...) @unhandled ||= [] # do something with unhandled end def do_something(..) @handlers ||= {} @unhandled ||= [] @all_handlers ||= [] # potentially do something with any of the 3 above end end Notice

Dynamic class names in LESS

自作多情 提交于 2019-12-03 12:13:11
I have the following bit of LESS code working @iterations: 940; @iterations: 940; @col:2.0833333333333333333333333333333%; // helper class, will never show up in resulting css // will be called as long the index is above 0 .loopingClass (@index) when (@index > -20) { // create the actual css selector, example will result in // .myclass_30, .myclass_28, .... , .myclass_1 (~".gs@{index}") { // your resulting css width: (@index/20+1)*@col; } // next iteration .loopingClass(@index - 60); } // end the loop when index is 0 .loopingClass (-20) {} // "call" the loopingClass the first time with highest

LESS mix-in for nth-child?

此生再无相见时 提交于 2019-12-03 11:37:42
问题 I'm trying to make a LESS mixin that will give me this output: .resource:nth-child(8n+1) { clear: left; } I've got this so far: .wrap-every(@n) { &:nth-child(@n + "n+1") { // parse error on this line clear: left; } } .resource { .wrap-every(8); } But it's giving a parse error on the indicated line ParseError: Unrecognised input Is there a way to do this? 回答1: Less >= 1.4 you could do something like this: .wrap-every(@n) { &:nth-child(@{n}n + 1) { clear: left; } } this should have the desired

Abstract class + mixin + multiple inheritance in python

社会主义新天地 提交于 2019-12-03 10:58:30
问题 So, I think the code probably explains what I'm trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class bar_for_foo_mixin(object): def bar(self): print "This should satisfy the abstract method requirement" class myfoo(foo, bar_for_foo_mixin): def __init__(self): print "myfoo __init__ called" self.bar() obj = myfoo() The result: TypeError: Can't instantiate abstract class myfoo with abstract

Doing math on variable argument Sass mixins

会有一股神秘感。 提交于 2019-12-03 10:47:09
I like to use rem units with pixel fallbacks for my CSS sizing and am trying to make mixins to help with that. For font-size, this is easy: @mixin font-size($size) { font-size: $size + px; font-size: ($size / 10) + rem; } But for padding, margin, etc. the mixin needs to accept variable arguments, which is possible per the Sass documentation http://sass-lang.com/documentation/file.SASS_REFERENCE.html#variable_arguments However, with the following mixin, instead of dividing by 10, the mixin is just adding a slash between the numbers. That is, this: @mixin padding($padding...) { padding: $padding