当我阅读不同的Ruby书籍时,我注意到Ruby类可以在其他Ruby类或模块中定义.以下是类中类的示例:
class Outerclass
def foobar
puts "FOOBAR"
end
class Innerclass
def barfoo
puts "BARFOO"
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我在IRB中运行的一些代码,试图从概念上理解这一点:
oc = Outerclass.new # => #<Outerclass:0x00000100a46478>
Outerclass.instance_methods(false) # => [:foobar]
ic = Outerclass::Innerclass.new # => #<Outerclass::Innerclass:0x00000100a0b120>
ic = Outerclass::Innerclass.instance_methods(false) # => [:barfoo]
Run Code Online (Sandbox Code Playgroud)
这是我的问题:
当ruby解释器第一次遇到我上面的类定义代码时,它是否会通过我编写的方法并将其存储在某个地方?我知道实例方法"foobar"实际上并没有运行,因为在Outerclass定义中没有调用它.
撇开第一个问题,当Ruby遇到类Innerclass时怎么样?这里发生了什么?
一般来说,为什么你想要在类中有类?这样做有什么好处吗?
Outerclass的实例是否知道有关类Innerclass的任何内容?
Innerclass的一个实例是否知道类Outerclass的任何内容?
感谢您提供的任何帮助!
这是原始SO问题的延续:使用"::"而不是"module ..."来表示Ruby命名空间
在最初的SO问题中,这里是我仍然无法理解的情景:
FOO = 123
module Foo
FOO = 555
end
module Foo
class Bar
def baz
puts FOO
end
end
end
class Foo::Bar
def glorf
puts FOO
end
end
puts Foo::Bar.new.baz # -> 555
puts Foo::Bar.new.glorf # -> 123
Run Code Online (Sandbox Code Playgroud)
有人可以提供一些解释为什么第一个呼叫返回555以及为什么第二个呼叫返回123?
我对Clojure很新,我不确定我是否完全理解Clojure中撇号和反引号之间的区别.
(def x 5)
;; Question 1
(+ x x) ;; evaluates to 10
'(+ x x) ;; evaluates to (+ x x)
`(+ x x) ;; evaluates to (clojure.core/+ user/x user/x)
;; Question 2
`(~+ ~x ~x) ;; evaluates to (#<core$_PLUS_ clojure.core$_PLUS_@32ee28a9> 5 5)
Run Code Online (Sandbox Code Playgroud)
我很高兴现在通过Babeljs使用ECMAScript 6功能 - 特别是,我很乐意使用新模块功能开始使我的JavaScript代码更加模块化.
这是我到目前为止所写的内容:
// ECMAScript 6 code - lib.js
export const sqrt = Math.sqrt;
export function square (x) {
return x * x;
}
export function diag (x, y) {
return sqrt(square(x) + square(y));
}
// ECMAScript 6 code - main.js
import { square, diag } from 'lib';
console.log(square(11));
console.log(diag(4, 3));
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以通过命令行上的babel将此代码从ES6转换为ES5:
babel lib.js > lib6to5.js
babel main.js > main6to5.js
Run Code Online (Sandbox Code Playgroud)
但是,在HTML中使用此代码需要做什么?
例如,这个index.html文件是什么样的:
<!-- index.html -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ECMAScript 6</title>
<!-- What goes here?
How …Run Code Online (Sandbox Code Playgroud) 我正在从"Metaprogramming Ruby"这本书中学习Ruby对象模型,我也理解了类如何成为对象的概念.
class A
end
A.ancestors # => [A, Object, Kernel, BasicObject]
A.class.ancestors # => [Class, Module, Object, Kernel, BasicObject]
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,当我最初在irb中尝试A.ancestors时,我期待我在A.class.ancestors中得到的结果 - 我的思考过程是:因为A是一个类,而一个类是一个实例class Class,它的祖先是Class.但是Class似乎不是A的祖先.
有人能在这里解决我的困惑吗?
我注意到很多处理Ruby Procs的例子都有以下&符号.
# Ruby Example
shout = Proc.new { puts 'Yolo!' }
def shout_n_times(n, &callback)
n.times do
callback.call
end
end
shout_n_times(3, &shout)
# prints 'Yolo!' 3 times
Run Code Online (Sandbox Code Playgroud)
我的问题是&符号背后的功能目的是什么?似乎如果我没有&编写相同的确切代码,它按预期工作:
# Same code as previous without &
shout = Proc.new { puts 'Yolo!' }
def shout_n_times(n, callback)
n.times do
callback.call
end
end
shout_n_times(3, shout)
# prints 'Yolo!' 3 times
Run Code Online (Sandbox Code Playgroud) 我明白使用Object.create(null)创建一个没有proto属性的对象(即Object.getPrototypeOf( myObj ) === null)但有人可以帮助我理解这个用例是什么?
换句话说,为什么要创建一个完全为空的对象(即不从中继承任何方法Object.prototype)?
仅使用React管理状态
我了解如果您只使用React创建应用程序,您将最终管理您创建的不同React组件中的所有状态.
使用React和Redux管理状态
如果您决定将Redux与React结合使用,则可以将每个React组件的所有状态移动到整个Redux应用程序状态.然后,需要切片Redux应用程序状态的每个组件都可以通过React-Redux的connect函数挂钩到状态.
题
这是否意味着您不再需要编写任何处理React state(即this.setState)connect的React组件,因为React-Redux 通过将数据传递到container组件中来处理具有Redux状态的React组件props?
测试异步调用componentDidMount的最佳方法是什么设置React组件的状态?对于情况下,我使用的测试库Mocha,Chai,Enzyme,和Sinon.
这是一个示例代码:
/*
* assume a record looks like this:
* { id: number, name: string, utility: number }
*/
// asyncComponent.js
class AsyncComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
records: []
};
}
componentDidMount() {
// assume that I'm using a library like `superagent` to make ajax calls that returns Promises
request.get('/some/url/that/returns/my/data').then((data) => {
this.setState({
records: data.records
});
});
}
render() {
return ( …Run Code Online (Sandbox Code Playgroud) 我创建了一个Github仓库,其中包含与此问题相关的所有源代码.我可以build/index.html在运行后加载浏览器npm install.但是,当我在Chrome中查看"来源"标签时,我看不到与.tsx文件关联的源地图.
我已经添加sourceMap: true在我tsconfig.json以及debug: true和devtool: "source-map"我webpack.config.json.对我在这里缺少什么的想法?
编辑:
这可能是一个愚蠢的问题,但你必须使用webpack-dev-server才能看到源图吗?
javascript ×4
ruby ×4
reactjs ×3
apostrophe ×1
babeljs ×1
backticks ×1
chai ×1
clojure ×1
ecmascript-6 ×1
enzyme ×1
inheritance ×1
mocha.js ×1
proc ×1
prototype ×1
react-redux ×1
redux ×1
source-maps ×1
typescript ×1
webpack ×1