Search By Label
console.dir(body, { depth: null });
// @ts-nocheck
// @ts-expect-error
class User def discounted_plan_price(discount_code) coupon = Coupon.new(discount_code) coupon.discount(account.plan.price) end end
account.plan.price
above violates the Law of Demeter by invoking price
on the return value of plan
. The price
method is not a method on User
, its parameter discount_code
, its instantiated object coupon
or its direct component account
.class User def discounted_plan_price(discount_code) account.discounted_plan_price(discount_code) end end class Account def discounted_plan_price(discount_code) coupon= Coupon.new(discount_code) coupon.discount(plan.price) end end
delegate
class method:class User delegate :discount_plan_price, to: :account end
languages = ["English", "Spanish", "French"] current_lang = "Spanish" try: # Use the index() method to find the index of the element index = languages.index(current_lang) print(f"The index of '{current_lang}' in the languages list is: {index}") except ValueError: print(f"'{current_lang}' is not found in the languages list.")
connect
package from the registry, you would run:npm view connect
npm view connect versions
json.dumps()
JSON.stringify()
, Python offers json.dumps()
to convert a Python object into a JSON string. This is the preferred method for most scenarios as JSON is a widely used and well-structured format.import json my_object = {"name": "Alice", "age": 30, "city": "New York"} json_string = json.dumps(my_object) print(json_string) # Output: {"name": "Alice", "age": 30, "city": "New York"}
docker image ls
runtime.txt
file to your app’s root directory that declares the exact version number to use.$ cat runtime.txt
python-3.12.4
enumerate()
.xs = [8, 23, 45] for index, x in enumerate(xs): print(f"item #{index + 1} = {x}")
item #1 = 8 item #2 = 23 item #3 = 45
function calculatePrice(total) { if (total > 1000) { return total * 0.9; // 0.9 is a magic number } else { return total * 0.95; // 0.95 is another magic number } }
0.9
and 0.95
are magic numbers because their meaning is not clear. If someone else reads this code in the future, it will be difficult to understand their significance without additional context.function getErrorMessage(code) {
switch (code) {
case "ERR001":
return "Connection Error"; // "ERR001" is a magic string
case "ERR002":
return "Authentication Error"; // "ERR002" is another magic string
default:
return "Unknown Error";
}
}
<%= button_to "Delete", [quote, line_item_date] %>
<%= button_to "Delete", quote_line_item_date_path(quote, line_item_date) %>
redirect_to @quote
redirect_to quote_path(@quote)