inject

Ruby inject until sum exceeds a set value and return the index where this occurs

我与影子孤独终老i 提交于 2019-12-04 16:24:53
With the following array and value: v = 50 a = [10, 20, 25, 10, 15] I want to iterate through the array adding up the values until the sum of these values exceeds the variable v. And then I want to be able to return the index in the array where that occurred. so... 10 + 20 + 25 = 55 (which is the first point which the sum is greater that 'v') so index = 2 Thanks for your help For the sum: a.inject do |sum,n| break sum if sum > v sum + n end For the index, idea is the same - you use the memo as an array and keep the sum in the first element: a.inject([0,-1]) do |memo,n| break [memo[0], memo[1]]

Use Spring to inject text file directly to String

不想你离开。 提交于 2019-12-04 15:50:11
问题 So I have this @Value("classpath:choice-test.html") private Resource sampleHtml; private String sampleHtmlData; @Before public void readFile() throws IOException { sampleHtmlData = IOUtils.toString(sampleHtml.getInputStream()); } What I'd like to know is if it's possible to not have the readFile() method and have sampleHtmlData be injected with the contents of the file. If not I'll just have to live with this but it would be a nice shortcut. 回答1: Technically you can do this with XML and an

http_sub_module / sub_filter of nginx and reverse proxy not working

▼魔方 西西 提交于 2019-12-04 11:11:53
问题 I am trying to reverse proxy my website and modify the content. To do so, I compiled nginx with sub_filter. It now accepts the sub_filter directive, but it does not work somehow. server { listen 8080; server_name www.xxx.com; access_log /var/log/nginx/www.goparts.access.log main; error_log /var/log/nginx/www.goparts.error.log; root /usr/share/nginx/html; index index.html index.htm; ## send request back to apache1 ## location / { sub_filter <title> '<title>test</title>'; sub_filter_once on;

Java EE 6 @Inject lazy? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:13:14
This question already has an answer here: How to make a CDI bean lazily initialized? 3 answers I am doing some refactoring and reviewing of the application that we are currently developing. While doing this I found that more beans are injected and I think making they loading in an lazy manner would be better suited for their purpose. I am using Java EE 6 and tend to use more CDI than EJB injection. So the question is whether it is possible to inject the beans lazily and if so, how can you do it? What about using @Inject private Instance<?> lazyProvider; ? That allows Getting instance of "?"

CDI Replacement for @ManagedProperty

☆樱花仙子☆ 提交于 2019-12-04 06:35:45
I'm very new to both CDI and JSF, and I'm trying to convert some code from Richfaces 4 showcase to use CDI instead of JSF annotations. I understand that I can use @Named to replace @MangedBean and @Inject to replace @ManagedProperty. But I'm having some trouble. I'm trying to convert the Richfaces Tree example specifically. I have made the following changes and I know this is not correct so please don't use this: //@ManagedBean //@ViewScoped @Named @SessionScoped public class TreeBean implements Serializable { private static final long serialVersionUID = 1L; // @ManagedProperty(value = "#

Add some block to product view page through module xml file Magento

ⅰ亾dé卋堺 提交于 2019-12-04 02:27:30
问题 Hi i am developing a simple extension in which i need to insert a new block on product page through xml file. Below is the xml file of my module <layout version="0.1.0"> <total_index_index> <reference name="root"> <action method="setTemplate"><template>page/2columns right.phtml</template></action> </reference> <reference name="content"> <block type="total/prototal" name="total_prototal" template="total.phtml" /> </reference> </total_index_index> </layout> In this, layout is working on module

golang: Martini之inject源码分析

旧城冷巷雨未停 提交于 2019-12-04 02:20:29
依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念。在传统的程序设计过程中, 调用者是自己来决定使用哪些被调用者实现的。 但是在依赖注入模式中, 创建被调用者的工作不再由调用者来完成,因此称为控制反转; 创建被调用者实例的工作通常由注入器来完成,然后注入调用者,因此也称为依赖注入。 inject 是依赖注入的golang实现,作者是 codegangsta 。它能在运行时注入参数,调用方法。是Martini框架的基础核心。 我对依赖注入提取了以下2点性质: 由注入器注入属性。 由注入器创建被调用者实例。 在inject中,被调用者为func,因此注入属性也即对func注入实参(当然inject也可以注入struct,这样的话注入的属性就是struct中的已添加tag为`inject`的导出字段)。我们来看下普通的函数调用: package main import ( "fmt" ) func Say(name, gender string, age int) { fmt.Printf("My name is %s, gender is %s, age is %d!\n", name, gender, age) } func main() { Say("陈一回", "男", 20) } 上面的例子中

How to inject HttpClient in static method or custom class?

余生颓废 提交于 2019-12-03 17:11:36
问题 I'd like to use angular HttpClient in static method or class (in class it can't be defined as constructor parameter). I tried something like: export class SomeNotInjectableService { static doSomething() { const injector = Injector.create({ providers: [{provide: HttpClient, deps:[]}] }); const httpClient: HttpClient = injector.get(HttpClient); httpClient.request(...); // error Cannot read property 'handle' of undefined } } this is a try of injecting manually the client in static service method

Understanding the behaviour of inject used with a lambda in Ruby

允我心安 提交于 2019-12-03 15:58:04
I often plug pre-configured lambdas into enumerable methods like 'map', 'select' etc. but the behavior of 'inject' seems to be different. e.g. with mult4 = lambda {|item| item * 4 } then (5..10).map &mult4 gives me [20, 24, 28, 32, 36, 40] However, if I make a 2-parameter lambda for use with an inject like so, multL = lambda {|product, n| product * n } I want to be able to say (5..10).inject(2) &multL since 'inject' has an optional single parameter for the initial value, but that gives me ... irb(main):027:0> (5..10).inject(2) &multL LocalJumpError: no block given from (irb):27:in `inject'

Mockito @InjectMocks doesn't work for fields with same type

笑着哭i 提交于 2019-12-03 15:39:29
问题 I was very surprised to find out that following simple code example doesn't work for all Mockito versions > 1.8.5 @RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Mock(name = "b2") private B b2; @InjectMocks private A a; @Test public void testInjection() throws Exception { assertNotNull(a.b2); //fails assertNull(a.b1); //also fails, because unexpectedly b2 mock gets injected here } static class A{ private B b1; private B b2; } interface B{} } In javadocs (http://docs.mockito