mixins

Ruby: Module, Mixins and Blocks confusing?

本小妞迷上赌 提交于 2019-12-01 11:19:28
Following is the code I tried to run from the Ruby Programming Book http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Why doesn't the product method give the right output? I ran it with irb test.rb . And I am running Ruby 1.9.3p194 . module Inject def inject(n) each do |value| n = yield(n, value) end n end def sum(initial = 0) inject(initial) { |n, value| n + value } end def product(initial = 1) inject(initial) { |n, value| n * value } end end class Array include Inject end [1, 2, 3, 4, 5].sum ## 15 [1, 2, 3, 4, 5].product ## [[1], [2], [3], [4], [5]] By the way: in Ruby 2.0,

How to inject same context in many different Django views?

做~自己de王妃 提交于 2019-12-01 11:00:52
I want to put info about one object in many views without repeating it in get_context_data in each view. As u understand i need a class with get_context_data inside, that i can mix with other views. Here in my example i want to see 'some_object' in context of UpdateAnotherObjectView: class BaseObjectInfoView(View): def get_context_data(self, **kwargs): context_data = super(BaseObjectInfoView, self).get_context_data(**kwargs) context_data['some_object'] = SomeObjects.objects.get(pk=1) return context_data class UpdateAnotherObjectView(BaseObjectInfo, UpdateView): template_name = 'create_object

Ruby: Module, Mixins and Blocks confusing?

瘦欲@ 提交于 2019-12-01 08:28:14
问题 Following is the code I tried to run from the Ruby Programming Book http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Why doesn't the product method give the right output? I ran it with irb test.rb . And I am running Ruby 1.9.3p194 . module Inject def inject(n) each do |value| n = yield(n, value) end n end def sum(initial = 0) inject(initial) { |n, value| n + value } end def product(initial = 1) inject(initial) { |n, value| n * value } end end class Array include Inject end

How to inject same context in many different Django views?

不羁的心 提交于 2019-12-01 07:52:01
问题 I want to put info about one object in many views without repeating it in get_context_data in each view. As u understand i need a class with get_context_data inside, that i can mix with other views. Here in my example i want to see 'some_object' in context of UpdateAnotherObjectView: class BaseObjectInfoView(View): def get_context_data(self, **kwargs): context_data = super(BaseObjectInfoView, self).get_context_data(**kwargs) context_data['some_object'] = SomeObjects.objects.get(pk=1) return

Two different mixin patterns in C++. (mixin? CRTP?)

天大地大妈咪最大 提交于 2019-11-30 17:42:18
I'm studying about mixins (in C++). I read some articles on mixins and found two different patterns of "approximating" mixins in C++. Pattern 1: template<class Base> struct Mixin1 : public Base { }; template<class Base> struct Mixin2 : public Base { }; struct MyType { }; typedef Mixin2<Mixin1<MyType>> MyTypeWithMixins; Pattern 2: (may be called CRTP) template<class T> struct Mixin1 { }; template<class T> struct Mixin2 { }; struct MyType { }; struct MyTypeWithMixins : public MyType, public Mixin1<MyTypeWithMixins>, public Mixin2<MyTypeWithMixins> { }; Are they equivalent practically? I'd like

Two different mixin patterns in C++. (mixin? CRTP?)

一曲冷凌霜 提交于 2019-11-30 16:44:26
问题 I'm studying about mixins (in C++). I read some articles on mixins and found two different patterns of "approximating" mixins in C++. Pattern 1: template<class Base> struct Mixin1 : public Base { }; template<class Base> struct Mixin2 : public Base { }; struct MyType { }; typedef Mixin2<Mixin1<MyType>> MyTypeWithMixins; Pattern 2: (may be called CRTP) template<class T> struct Mixin1 { }; template<class T> struct Mixin2 { }; struct MyType { }; struct MyTypeWithMixins : public MyType, public

Where to put common code found in multiple models?

痞子三分冷 提交于 2019-11-30 11:54:40
问题 I have two models that contain the same method: def foo # do something end Where should I put this? I know common code goes in the lib directory in a Rails app. But if I put it in a new class in lib called ' Foo ', and I need to add its functionality to both of my ActiveRecord models , do I do that like this: class A < ActiveRecord::Base includes Foo class B < ActiveRecord::Base includes Foo and then both A and B will contain the foo method just as if I had defined it in each? 回答1: Create a

What is the difference between 'include' and 'prepend' in Ruby?

半腔热情 提交于 2019-11-30 11:19:45
From the Module Module#append_features(mod) → mod => When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. Module#prepend_features(mod) → mod => When this module is prepended in another, Ruby calls prepend_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to overlay the constants, methods, and module

Using mixins vs components for code reuse in Facebook React

久未见 提交于 2019-11-30 10:05:50
问题 I'm beginning to use Facebook React in a Backbone project and so far it's going really well. However, I noticed some duplication creeping into my React code. For example, I have several form-like widgets with states like INITIAL , SENDING and SENT . When a button is pressed, the form needs to be validated, a request is made, and then state is updated. State is kept inside React this.state of course, along with field values. If these were Backbone views, I would have extracted a base class

Combine extend and mixin in with same rules

浪子不回头ぞ 提交于 2019-11-30 10:03:09
问题 Okey! I have couple of extends in sass like %heading %paragraph %gutter and so on... I want to reuse thouse in media queries, but that doesnt work. I know that. Then i came up with the idea to have all my extends as mixins too. So when i want them in a media query i simply use mixin. for example .my-widget { @extend %gutter; @media.... { @include gutter-other; } } and because i dont want to write all my rules again. How do i write my sass then? I tried %my-extend, @mixin my-extend { ... } but