require

Circular Dependencies in Ruby

旧巷老猫 提交于 2019-11-28 00:10:09
问题 Let's say we have two classes, Foo and Foo Sub, each in a different file, foo.rb and foo_sub.rb respectively. foo.rb: require "foo_sub" class Foo def foo FooSub.SOME_CONSTANT end end foo_sub.rb: require "foo" class FooSub < Foo SOME_CONSTANT = 1 end This isn't going to work due to the circular dependency - we can't define either class without the other. There are various solutions that I've seen. Two of them I want to avoid - namely, putting them in the same file and removing the circular

构建基于Node.js的web应用

孤人 提交于 2019-11-27 22:34:35
构建基于Node.js的web应用 (参考书籍《node入门》) 在写Node.js应用的时候我们不仅要知道 如何写代码 ,还要知道 如何组织这些代码 。 下面我们来看一个简单的基于Node.js的web应用,以此来学习如何组织我们项目的代码,这个应用的主要功能是上传图片,并在浏览器中展示出来。 应用的结构 我们来看看整个 应用结构 : project +----node_modules |----router.js |----server.js |----requestHandles.js |----index.js node_modules中存放的是项目相关的依赖。 router.js为路由,以便我们队不同的url进行不同的处理。 requestHandles.js是对不同url进行的具体的处理。 server.js为服务器模块,我们将在这里创建服务器。 index.js为整个项目的入口文件。 这个web应用,它的代码并不是简单的凑到一块,而是有一定的结构,是模块化的组织方式,不同的功能对应不同的模块(文件),这有利于代码的维护和重构,在大项目中也有利于团队的合作。 现在我们来具体到每个文件,一点一点的分析这个web应用。 1. 先来看一下server.js文件: var http = require("http"); var url = require("url")

Swift REPL: how to import, load, evaluate, or require a .swift file?

守給你的承諾、 提交于 2019-11-27 21:54:36
In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file? I want to use the code from this file: ~/src/Foo.swift Syntax like this doesn't work: import ~/src/Foo.swift For comparison: An equivalent solution in the Swift REPL for a framework is: import Foundation An equivalent solution in the Ruby REPL for a *.ruby file is: require "~/src/foo" These are similar questions that are /not/ what I'm asking: How to use/make a Swift command-line script, executable, module, etc. How to use/make an XCode playground, project, library, framework, etc. How to launch the

【珍惜时间】iReport

半城伤御伤魂 提交于 2019-11-27 21:51:34
项目很点意思,感觉很高超的样子 先放下项目的github地址:https://github.com/tctangyanan/iReport 感谢各位伟大的程序员无私的分享自己的技术 老规矩,我们会运行项目 页面效果为 先逐行分析代码 先看main.js,引入了我们需要的一些全局插件 //main.js import Vue from 'vue' import 'normalize.css/normalize.css'// A modern alternative to CSS resets import App from './App' import router from './router' // import routes from './router/routes' import store from './vuex/store' import { sync } from 'vuex-router-sync' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import './styles/index.scss' // global css // 进度条 import NProgress from 'nprogress' // progress bar

webpack多页面配置

江枫思渺然 提交于 2019-11-27 21:05:36
webpack.common.js const webpack = require("webpack"); const path = require('path'); const glob = require('glob'); const HtmlWebpackPlugin = require('html-webpack-plugin'); // html引擎 const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); let outputfile = '' function buildEntriesAndHTML() { // 用来构建entry const result = glob.sync('views/**/*.js'); const config = { hash: false, inject: true }; const entries = {}; const htmls = [

How to do multiple imports in Python?

点点圈 提交于 2019-11-27 20:28:29
In Ruby, instead of repeating the "require" (the "import" in Python) word lots of times, I do %w{lib1 lib2 lib3 lib4 lib5}.each { |x| require x } So it iterates over the set of "libs" and "require" (import) each one of them. Now I'm writing a Python script and I would like to do something like that. Is there a way to, or do I need to write "import" for all of them. The straight-forward "traduction" would be something like the following code. Anyway, since Python does not import libs named as strings, it does not work. requirements = [lib1, lib2, lib3, lib4, lib5] for lib in requirements:

Getting closure-compiler and Node.js to play nice

﹥>﹥吖頭↗ 提交于 2019-11-27 20:05:30
Are there any projects that used node.js and closure-compiler (CC for short) together? The official CC recommendation is to compile all code for an application together, but when I compile some simple node.js code which contains a require("./MyLib.js") , that line is put directly into the output, but it doesn't make any sense in that context. I see a few options: Code the entire application as a single file. This solves the problem by avoiding it, but is bad for maintenance. Assume that all files will be concatenated before execution. Again this avoids the problem, but makes it harder to

how to require active record working outside of rails

纵饮孤独 提交于 2019-11-27 19:21:58
i need to require active record, but I am working outside of rails (here is why: Simple Ruby Input Validation Library ). do I need to require the entire rails gem, or can i be DRYer? Paweł Gościcki Here's how I'm using ActiveRecord outside of Rails: #!/usr/bin/ruby require 'active_record' require 'mysql2' # or 'pg' or 'sqlite3' ActiveRecord::Base.establish_connection( adapter: 'mysql2', # or 'postgresql' or 'sqlite3' database: 'DB_NAME', username: 'DB_USER', password: 'DB_PASS', host: 'localhost' ) # Note that the corresponding table is 'orders' class Order < ActiveRecord::Base end Order.all

百度ai图像识别

◇◆丶佛笑我妖孽 提交于 2019-11-27 18:10:49
require 'restclient' require 'open-uri' require 'nokogiri' require 'json' require 'yaml' require 'fileutils' require 'base64' def extract_phone(image_src) url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" access_token = "24.0bbbc3338fcf9df41f26b42ff2a077.2593200.15323228.282335-11449805" content_type = "application/x-www-form-urlencoded" file = open(image_src).read image = Base64.encode64(file) RestClient.post(url, {access_token: access_token, image: image}, {content_type: content_type}) do |response| body = JSON.parse(response.body) return raise unless body['error_code'].nil?

Conditional build based on environment using Webpack

丶灬走出姿态 提交于 2019-11-27 17:34:20
I have some things for development - e.g mocks which I would like to not bloat my distributed build file with. In RequireJS you can pass a config in a plugin file and conditonally require things in based on that. For webpack there doesn't seem to be a way of doing this. Firstly to create a runtime config for an environment I have used resolve.alias to repoint a require depending on the environment, e.g: // All settings. var all = { fish: 'salmon' }; // `envsettings` is an alias resolved at build time. module.exports = Object.assign(all, require('envsettings')); Then when creating the webpack