所以我知道一些我所知道的不同方法,并且我想探索各种方法的优缺点,这些方法有以下几种:
从主动支持中明确使用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) 我试图在以下元素上使用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)
但是没有工作.这可能吗?
我的文件中有以下导入内容:
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)