mixins

Does Objective-C support Mixin like Ruby?

无人久伴 提交于 2019-11-30 00:02:36
In Ruby, there's Modules and you can extend a class by "mixing-in" the module. module MyModule def printone print "one" end end class MyClass include MyModule end theOne = MyClass.new theOne.printone >> one In Objective-C, I find that I have a set of common methods that I want a number of Class to "inherit". What other ways can I achieve this without creating a common class and deriving all from that common class? Jean-Denis Muys Edit : changes added because some people feel I am responsible for the limitations of Objective-C. Short answer : you can't. Objective-C doesn't have the equivalent

Ruby: module, require and include

时光毁灭记忆、已成空白 提交于 2019-11-29 22:06:21
I'm trying to use Ruby modules (mixins). I have test.rb: #!/usr/bin/env ruby require_relative 'lib/mymodule' class MyApp include MyModule self.hallo end and lib/mymodule.rb: module MyModule def hallo puts "hallo" end end Quite simple setup. But it does not work :( : ruby test.rb test.rb:8:in `<class:MyApp>': undefined method `hallo' for MyApp:Class (NoMethodError) from test.rb:6:in `<main>' Where is my error? In short: you need to extend instead of include the module. class MyApp extend MyModule self.hallo end include provides instance methods for the class that mixes it in. extend provides

Using mixins vs components for code reuse in Facebook React

≡放荡痞女 提交于 2019-11-29 18:43:23
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 called FormView but my impression was that React neither endorses nor supports subclassing to share view

ES 6 Classes - Mixins

自古美人都是妖i 提交于 2019-11-29 18:03:45
I'm coming up with View (HTML markup) and Utility (JavaScript - behavior) architecture and creating atomic classes for composing views and utilities using ES6 Class. There will be a need that multiple utility classes can be composed/mixed into a single view class. How can ES6 Class API provide a way to mix-in class(es) into another/main class. I've looked at Object.assign but that is for objects and not at class-level. JavaScript classes right now and hopefully also in future only can be extended from each other but can not be mixed into one another. If at all, then most probably Lightweight

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

核能气质少年 提交于 2019-11-29 17:21:09
问题 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

LESS mixin recursion error to convert pixels to rems

不问归期 提交于 2019-11-29 17:12:14
I am trying to make a mixin to propery convert pixels to relative ems. I would like it to be flexible enough to allow any property to be used with any number of pixel values. Any ideas on how to add multiple values to a single property without the recursion error I'm creating inside the for loop? desired usage example 1: .pixels-to-rems(font-size; 10); desired output: font-size: 10px; font-size: 1rem; desired usage example 2: .pixels-to-rems(padding; 10,0,20,10); desired output: padding: 10px, 0px, 20px, 10px; padding: 1rem, 0px, 2rem, 1rem; Here's the mixin as is. @baseFontSize: 10px; .pixels

When does for call the iterator method?

痴心易碎 提交于 2019-11-29 15:07:28
This question is in the same ballpark as this other on making blocks iterable , but seems to reveal a different problem with mixins (or a different misunderstanding of the syntax on my part). What Iterable does is to make a data structure effectively iterable, that is, you can create loops by preceding it with for . Iterable serves as an API for objects that can be iterated with the for construct and related iteration constructs, like hyper operators. So let's try to put this to practice: my &logger = -> $event { state %store; if ( $event ) { %store{ DateTime.new( now ) } = $event; } else {

Why a module's singleton method is not visible in downstream eigenclasses where it gets mixed?

北慕城南 提交于 2019-11-29 14:04:53
I understand the regular method lookup path i.e. class, superclass/module, all the way up to BasicObject . I thought it was true for singleton version of the chain also but doesn't seem the case when you mixin a module in the meta-chain. I'd appreciate if someone can explain why in the following example Automobile module's banner method is called instead of its singleton version when I have included this module in Vehicle's eigenclass. module Automobile def banner "I am a regular method of Automobile" end class << self def banner "I am a singleton method of Automobile" end end end class

LESS CSS: Reuse generated .@{name} class as a mixin

我只是一个虾纸丫 提交于 2019-11-29 09:35:40
问题 I'm using LESS CSS 1.3.3. Sorry if this question has already been asked, I didn't find anything relevant on the web. I have several class generators that look like this (example extremely simplified, just enough to trigger the error): #genMarginTop (@name, @size) { .@{name} { margin-top: @size; } } Then I use them to generate some actual classes: #genMarginTop(mtStandard, 40px); #genMarginTop(mtHalf, 20px); So far, so good, LESS correctly generates those classes and I can use them in the HTML

Sass extend with pseudo selectors

人盡茶涼 提交于 2019-11-29 07:23:43
I am using compass to manage some sass files on mac osx. I have these files: sass/ screen.scss partials folder/ ... _fonts.scss _functions.scss ... In fonts I have this rule which I would like to reuse @extend. //fonts.scss .icon-ab-logo, { font-family: 'icomoon'; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; line-height: 1; -webkit-font-smoothing: antialiased; } .icon-ab-logo:before { //i want to reuse this. content: "\e000"; } In functions I have this screen.scss: .logo { position: absolute; top: 0; bottom: 0px; z-index: 500; width: 9.5em;