我对 Ruby 很陌生,正在尝试读取文件的每一行。我想使用每一行创建一个名为 LineAnalyzer 的对象,然后将该对象添加到名为analyzers 的数组中。
我正在尝试的代码是
Class Solution
attr_reader :analyzers;
def initialize()
@analyzers = Array[];
end
def analyze_file()
count = 0;
f = File.open('test.txt')
#* Create an array of LineAnalyzers for each line in the file
f.each_line { |line| la = LineAnalyzer.new(line, count) }
@analyzers.push la;
count += 1;
end
end
end
Run Code Online (Sandbox Code Playgroud)
任何帮助或建议将不胜感激!
我试图将一个对象作为道具传递给子组件。该对象以 null 开始,并在用户从列表中选择项目时设置。奇怪的是,该对象第一次设置正确,但之后就没有了。
这是我的父组件:
@Component({
selector: 'my-app',
template: `
<div>
<search-bar (submitted)="onSearch($event)"></search-bar>
<div>
<video-detail [video]="selectedVideo"></video-detail>
<video-list [videos]="videos" [onVideoSelect]="onVideoSelect" ></video-list>
<div>
</div>
`
})
export default class AppComponent {
videos = [];
selectedVideo = null;
constructor(private youtube: YoutubeService) { }
onSearch(searchTerm: string) {
this.youtube.search(searchTerm).subscribe((response) => {
this.videos = response.items;
this.selectedVideo = response.items[0];
});
}
// called when the user clicks an item
onVideoSelect(video: object) {
this.selectedVideo = video;
}
}
Run Code Online (Sandbox Code Playgroud)
子组件:
const { Component, Input } = ng.core;
const { DomSanitizer …Run Code Online (Sandbox Code Playgroud) 我在一个带有 redux 的新 create-react-app react 项目中使用 redux-saga,出于某种原因,redux-saga 似乎没有接受调度的操作。减速器中的控制台日志语句显示操作确实到达减速器。关于这一切的奇怪之处在于,我将这个项目基于我成功使用 redux-saga 的其他项目(但不是 create-react-app 的)。我想知道这是版本问题还是需要一些我缺少的支持包。下面是一个完整的代码片段。
从我的 package.json 文件:
"dependencies": {
"axios": "^0.19.0",
"connect-history-api-fallback": "^1.6.0",
"connected-react-router": "^6.5.2",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-google-login": "^5.0.5",
"react-redux": "^7.1.1",
"react-router-dom": "^5.0.1",
"react-scripts": "3.1.1",
"redux": "^4.0.4",
"redux-actions": "^2.6.5",
"redux-immutable-state-invariant": "^2.1.0",
"redux-persist": "^5.10.0",
"redux-saga": "^1.0.5",
"shortid": "^2.2.14",
"styled-components": "^4.3.2",
"styled-modern-normalize": "^0.2.0"
},
"devDependencies": {
"react-hot-loader": "^4.12.11",
"history": "^4.9.0"
},
Run Code Online (Sandbox Code Playgroud)
最外面的 index.js 文件(我的应用程序的入口点):
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './components/App'; …Run Code Online (Sandbox Code Playgroud) 使用 svelte 编译器从合成文件(字符串)生成代码。其输出也是一个字符串,但它添加了 svelte 本身的导入语句。我需要从生成的代码中删除所有这些导入语句,但我还需要获取这些导入语句并以可以从 CDN 导入/要求的方式添加它们。
我打算将这些导入语句解析为:
import { SvelteComponent, append, etc. } from "svelte/internal";
Run Code Online (Sandbox Code Playgroud)
并将其替换为以下内容:
const { SvelteComponent, append, etc. } = svelte.internal;
Run Code Online (Sandbox Code Playgroud)
我最初的想法是,是否可以将代码内联添加到 js 模块中,以从纯 js / es6 的 CDN 导入/需要脚本(在本例中为 svelte)。该项目是用 React 编写的,我已经编写了自己的钩子来动态加载脚本,但我认为需要将其添加到 iframe 的窗口中。但回想一下模块是如何工作的,我相信将脚本添加到父窗口也是可行的。所以我的问题是,如何将脚本从 CDN 的导入添加到我的 javascript 模块中?
假设我有一个抽象基类,Shape,然后是三个派生类:Triangle,Rectangle和Pentagon.必须根据需求创建派生类,这样我就可以拥有每个派生类中的几个.基类有很多字段,我一直在寻找一种方法只设置一次基类属性,然后稍后为派生类设置不同的字段.既然你无法实例化一个抽象类,那么最好的方法是什么?我想也许创建另一个派生自Shape的类,它只具有Shape的属性,设置那些属性然后转换它但这看起来效率低下并且在它上面写了代码味道.有人可以提出更好的方法吗?谢谢.下面我写了一些针对这种情况的伪代码
public abstract class Shape
{
public int x, y, z;
}
public class Triangle : Shape
{
public int a, b, c
}
public class Facade : Shape
{
}
private Facade InitializeBaseProperties()
{
Facade f = new Training
{
x = 1, y = 2, z = 3
};
return f;
}
private void someMethod()
{
var tmp = InitializeBaseProperties();
Triangle triangle = tmp;
}
Run Code Online (Sandbox Code Playgroud) angular ×1
c# ×1
ecmascript-6 ×1
es6-modules ×1
javascript ×1
reactjs ×1
redux ×1
redux-saga ×1
ruby ×1