4 ms·
In Ruby, everything evaluates to true except false and nil. Also, the last statement executed is your return value. Therefore your first function could be wri
by waffle_ss 15y ago
In Ruby, everything evaluates to true except false and nil. Also, the last statement executed is your return value. Therefore your first function could be written as
def delete_student(uid)
STUDENTS.remove(uid) || raise WebApplicationException.new(Response.Status.NOT_FOUND)
end
- pacala 15y agoI have no idea what the annotations mean, but if we use exceptions for transmitting error conditions, then there is no point having a true | exception function. public void deleteStudent(int uid) { if (STUDENTS.remove(uid) == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } }
- smanek 15y agoI love it! And that is the reason I still write in Python or Lisp when I don't need something (Hadoop, Lucene, etc) from the JVM ;-)
- jblomo 15y agoIf you need something from the JVM and want to write Lisp, use Clojure. Checkout Hadoop extensions in Clojure at https://github.com/jblomo/oddjob https://github.com/jblomo/oddjob .
- mbell 15y agoI seem to remember this same feature causing lots of headache in C in the form of subtle bugs mostly the result of this being used in awkward ways. Personally I'd rather have a distinct boolean value to test for, the java version is much more explicit as to exactly what its doing.
- waffle_ss 15y agoHmm can't say I've ever encountered something like that in Ruby so far. However, if you want a strict boolean it's pretty easy to get: !!STUDENTS.remove(uid)
- jeltz 15y agoIn that case it is preferable to use "or" over "||" since then you do not have to worry so much about operator precedence because "or" has about the weakest of all operators. This means in this case that you can skip the explicit ".new" for the exception. def delete_student(uid) STUDENTS.remove(uid) or raise WebApplicationException, Response.Status.NOT_FOUND end
- latchkey 15y agoIn Ruby, you don't write code, code writes you! ;-)