小编jdf*_*ino的帖子

长零安全方法链

所以我知道一些我所知道的不同方法,并且我想探索各种方法的优缺点,这些方法有以下几种:

  • 可读性
  • 性能
  • 易于调试
  • OO原则(低耦合和高内聚)

从主动支持中明确使用try方法

person.try(:pet).try(:name).try(:upcase)
Run Code Online (Sandbox Code Playgroud)

使用救援无

person.pet.name.upcase rescue nil
Run Code Online (Sandbox Code Playgroud)

使用&&运算符链

person && person.pet && person.pet.name && person.pet.name.upcase
Run Code Online (Sandbox Code Playgroud)

猴子修补Object类,请参阅https://gist.github.com/thegrubbsian/3499234获取原始要点

 class Object

      def try_all(*methods)
        values = [self]
        methods.each do |method|
          value = values.last.try(method)
          return nil if value.nil?
          values << value
        end
        values.last
      end

  end

person.try_all(:pet, :name, :upcase)
Run Code Online (Sandbox Code Playgroud)

不要使用nil安全代码,而是在调用代码之前验证数据

#not a good implementation by any means    
def person_has_pet_with_name? person
  begin 
    return true if !person.pet.name.nil?
  rescue
    return false
  end
end

person.pet.name.upcase if person_has_pet_with_name?(person)
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails activesupport

5
推荐指数
1
解决办法
1062
查看次数

Geb + Groovy在属性名称中选择带有破折号的自定义元素

我试图在以下元素上使用Geb jQuery选择器:

<li data-title="Something"><'li>
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

//1
$("li", "data-title": "Something")
//2
$("li", data-title: "Something")
Run Code Online (Sandbox Code Playgroud)

但是没有工作.这可能吗?

groovy ui-automation geb

5
推荐指数
1
解决办法
1069
查看次数

导入应按字母顺序排序错误

我的文件中有以下导入内容:

import axios from "axios";
import lodash from "lodash";
import PropTypes from "prop-types";
import React from "react";
import renderInCustomHtmlTag from "react-render-custom-html-tag";
Run Code Online (Sandbox Code Playgroud)

我正在使用 v5.9.0,但由于某种原因,我在第 3 行收到错误,尽管它似乎是按字母顺序排列的。

我的eslint如下:

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es6" : true,
    "jquery": true,
    "node": true
  },
  "extends": [
    "eslint:all",
    "plugin:react/recommended"
  ],
  "globals": {
    "$cgx": true
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "sourceType": "module"
  },
  "plugins": [
    "import",
    "jsx-a11y",
    "react"

  ],
  "rules": {
    "indent": ["error", 2],
    "max-len": [
      "error",
      {
        "code": 120 …
Run Code Online (Sandbox Code Playgroud)

node.js eslint

3
推荐指数
1
解决办法
7824
查看次数