Checking if a variable is defined in SASS

For Sass 3.3 and later As of Sass 3.3 there is a variable-exists() function. From the changelog: It is now possible to determine the existence of different Sass constructs using these new functions: variable-exists($name) checks if a variable resolves in the current scope. global-variable-exists($name) checks if a global variable of the given name exists. … … Read more

What is a regex to match a string NOT at the end of a line?

/abc(?!$)/ (?!$) is a negative lookahead. It will look for any match of abc that is not directly followed by a $ (end of line) Tested against abcddee (match) dddeeeabc (no match) adfassdfabcs (match) fabcddee (match) applying it to your case: ruby-1.9.2-p290 :007 > “aslkdjfabcalskdfjaabcaabc”.gsub(/abc(?!$)/, ‘xyz’) => “aslkdjfxyzalskdfjaxyzaabc”

Check if URL exists in Ruby

Use the Net::HTTP library. require “net/http” url = URI.parse(“http://www.google.com/”) req = Net::HTTP.new(url.host, url.port) res = req.request_head(url.path) At this point res is a Net::HTTPResponse object containing the result of the request. You can then check the response code: do_something_with_it(url) if res.code == “200” Note: To check for https based url, use_ssl attribute should be true as: … Read more

How can I calculate the day of the week of a date in ruby?

I have used this because I hated to go to the Date docs to look up the strftime syntax, not finding it there and having to remember it is in the Time docs. require ‘date’ class Date def dayname DAYNAMES[self.wday] end def abbr_dayname ABBR_DAYNAMES[self.wday] end end today = Date.today puts today.dayname puts today.abbr_dayname

to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby

Note first that all of this applies to each pair of “short” (e.g. to_s/to_i/to_a/to_h) vs. “long” (e.g. to_str/to_int/to_ary/to_hash) coercion methods in Ruby (for their respective types) as they all have the same semantics. They have different meanings. You should not implement to_str unless your object acts like a string, rather than just being representable by … Read more

How do I stub things in MiniTest?

# Create a mock object book = MiniTest::Mock.new # Set the mock to expect :title, return “War and Piece” # (note that unless we call book.verify, minitest will # not check that :title was called) book.expect :title, “War and Piece” # Stub Book.new to return the mock object # (only within the scope of the … Read more