idioms

Check whether a variable is a string in Ruby

ぐ巨炮叔叔 提交于 2019-12-02 14:15:59
Is there anything more idiomatic than the following? foo.class == String I think you are looking for instance_of? . is_a? and kind_of? will return true for instances from derived classes. class X < String end foo = X.new foo.is_a? String # true foo.kind_of? String # true foo.instance_of? String # false foo.instance_of? X # true A more duck-typing approach would be to say foo.respond_to?(:to_str) to_str indicates that an object's class may not be an actual descendant of the String, but the object itself is very much string-like (stringy?). Federico Builes You can do: foo.instance_of?(String)

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

穿精又带淫゛_ 提交于 2019-12-02 13:55:55
What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience? An example: class A { public: char s[1024]; char *p; A::A() { p = s; } void changeS() const { p[0] = 'a'; } }; Even know changeS is a const member function, it is changing the value of the object. So a const member function only means that it will treat all variables as const, and it does not mean that it will actually keep all members const. (why? the const keyword on the member function treats char *p; as char * const p; And not as const char *p; Which therefore means that p can't point to

What is a function signature and type?

孤人 提交于 2019-12-02 13:28:43
问题 I found the type alias below in an Scheme interpreter I'm studying. While evaluating the AST, it recognizes a function either as a natively supported function, or as a user defined function. I understand the second part of the Enum definition, but the first part eludes me. pub enum Function { Native(ValueOperation), Scheme(Vec<String>, Vec<Value>, Rc<RefCell<Environment>>), } type ValueOperation = fn(&[Value], Rc<RefCell<Environment>>) -> Result<Value, RuntimeError>; How does this type alias

What is a function signature and type?

放肆的年华 提交于 2019-12-02 07:22:21
I found the type alias below in an Scheme interpreter I'm studying. While evaluating the AST, it recognizes a function either as a natively supported function, or as a user defined function. I understand the second part of the Enum definition, but the first part eludes me. pub enum Function { Native(ValueOperation), Scheme(Vec<String>, Vec<Value>, Rc<RefCell<Environment>>), } type ValueOperation = fn(&[Value], Rc<RefCell<Environment>>) -> Result<Value, RuntimeError>; How does this type alias work? Does this definition say that a ValueOperation is just a shorthand for a function signature? I

Is working past the end of a slice idiomatic?

≯℡__Kan透↙ 提交于 2019-12-02 00:27:27
I was reading through Go's compress/flate package, and I found this odd piece of code [1]: n := int32(len(list)) list = list[0 : n+1] list[n] = maxNode() In context, list is guaranteed to be pointing to an array with more data after. This is a private function, so it can't be misused outside the library. To me, this seems like a scary hack that should be a runtime exception. For example, the following D code generates a RangeError: auto x = [1, 2, 3]; auto y = x[0 .. 2]; y = y[0 .. 3]; Abusing slices could be done more simply (and also look more safe) with the following: x := []int{1, 2, 3} y

Idiomatic way to test if no positional params are given?

懵懂的女人 提交于 2019-12-01 20:46:17
问题 What is the most idiomatic way in Bash to test if no positional parameters are given? There are so many ways to check this, I wonder if there is one preferred way. Some ways are: ((! $# )) # check if $# is 'not true' (($# == 0)) # $# is 0 [[ ! $@ ]] # $@ is unset or null 回答1: For me, the classical way is: [[ $# -eq 0 ]] 回答2: If you want it to be an error to have no positional parameters: : ${@?no positional parameters} will print "no positional parameters" to standard error (and exit a non

Why use Python's “else” clause in try/except block? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-01 18:12:07
Possible Duplicate: Python try-else I'm not seeing the benefit of it, at least based on the example I just read in Dive Into Python: try: from EasyDialogs import AskPassword except ImportError: getpass = default_getpass else: getpass = AskPassword ( http://www.diveintopython.net/file_handling/index.html ) Why couldn't you achieve the same effect with the shorter/simpler: try: from EasyDialogs import AskPassword getpass = AskPassword except ImportError: getpass = default_getpass What am I missing? Izkata There isn't an advantage in the example, except possibly for style. It's generally a good

Why use Python's “else” clause in try/except block? [duplicate]

怎甘沉沦 提交于 2019-12-01 17:59:12
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Python try-else I'm not seeing the benefit of it, at least based on the example I just read in Dive Into Python: try: from EasyDialogs import AskPassword except ImportError: getpass = default_getpass else: getpass = AskPassword (http://www.diveintopython.net/file_handling/index.html) Why couldn't you achieve the same effect with the shorter/simpler: try: from EasyDialogs import AskPassword getpass = AskPassword

Is there an idiomatic way to get a potentially undefined key from an array in PHP?

一个人想着一个人 提交于 2019-12-01 16:49:43
PHPeoples, I'm so tired of doing this $value = isset($arr[$key]) ? $arr[$key] : null; Or this $value = array_key_exists($key, $arr) ? $arr[$key] : null; Don't nobody tell me to do $arr = array(1); $key = 5; $value = $arr[$key]; // Notice: Undefined offset: 5 I got bronchitis. Ain't nobody got time f'dat. I could make a function, I guess... function array_get(Array $arr, $key, $default=null) { return array_key_exists($key, $arr) ? $arr[$key] : $default ; } But is this the best (most idiomatic) way? More elegant way of doing it: function ifsetor(&$value, $default = null) { return isset($value) ?

Is there an idiomatic way to get a potentially undefined key from an array in PHP?

余生颓废 提交于 2019-12-01 15:30:51
问题 PHPeoples, I'm so tired of doing this $value = isset($arr[$key]) ? $arr[$key] : null; Or this $value = array_key_exists($key, $arr) ? $arr[$key] : null; Don't nobody tell me to do $arr = array(1); $key = 5; $value = $arr[$key]; // Notice: Undefined offset: 5 I got bronchitis. Ain't nobody got time f'dat. I could make a function, I guess... function array_get(Array $arr, $key, $default=null) { return array_key_exists($key, $arr) ? $arr[$key] : $default ; } But is this the best (most idiomatic)