Skip to content

Instantly share code, notes, and snippets.

@ascendbruce
ascendbruce / gist:7071173
Last active December 26, 2015 01:28
Temporarily Disable Executed SQL Command Output in Rails Console (Disable rails logger output)
ActiveRecord::Base.logger.level = Logger::INFO
# from http://lexsheehan.blogspot.tw/2013/04/temporarily-disable-executed-sql.html
@ascendbruce
ascendbruce / gist:7071165
Created October 20, 2013 15:34
Indifferent access an array with string/symbol keys or convert all keys to symbol
## with_indifferent_access
1.9.2p290 :018 > h = {:a=>1, "b"=>2, :c=>3}
# => {:a=>1, "b"=>2, :c=>3}
1.9.2p290 :019 > h = h.with_indifferent_access
# => {"a"=>1, "b"=>2, "c"=>3}
1.9.2p290 :020 > h["b"]
# => 2
1.9.2p290 :021 > h[:b]
# => 2
@ascendbruce
ascendbruce / gist:7071080
Created October 20, 2013 15:28
ruby 陽春模仿 1.year/month/day, Array#sum
# ruby Fixnum# year/month/day
class Fixnum
def minutes
self*60
end
def hours
self*60*60
end
def days
@ascendbruce
ascendbruce / gist:7071061
Created October 20, 2013 15:26
Ubuntu add an user to admin group using usermaod
usermod -a -G sudo USERNAME

or

usermod -a -G admin USERNAME

@ascendbruce
ascendbruce / gist:7071054
Last active December 26, 2015 01:28
Octopress commands cheatsheet

Create an new post draft

rake new_post["Zombie Ninjas Attack: A survivor's retrospective"]

Multiple categories example

categories: [CSS3, Sass, Media Queries]

rake tasks

@ascendbruce
ascendbruce / gist:7071046
Created October 20, 2013 15:24
ruby regexp string strip HTML
str.gsub(/<\/?[^>]*>/, "")
@ascendbruce
ascendbruce / gist:7071040
Created October 20, 2013 15:24
Ruby and ActiveRecord Timezone = Taipei setting
config.time_zone = 'Taipei' # For ActiveRecord (Still UTC in database)
ENV['TZ'] = 'Asia/Taipei' # For ruby
@ascendbruce
ascendbruce / gist:7071030
Created October 20, 2013 15:23
Meta tags and open graph og:tags examples
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="" name="description">
<meta content="" name="keywords">
<meta content="" property="og:image">
<meta content="" property="og:url">
<meta content="" property="og:site_name">
<meta content="" property="og:title">
<meta content="" property="og:type">
@ascendbruce
ascendbruce / ruby-how-to-check-string-valid-json-object.md
Last active December 22, 2023 06:39
Ruby test if a string is an valid json object

Background

I posted this gist a few years ago as a quick memo for myself. I never expect this little gist to rank up in top 3 search result and getting so many helpful feedbacks. Since it gets viewed quite often, I decided to revise this post so that it may be a little more helpful. Thank you again for the feedback!

The new answer

require "json"

class MyJSON
@ascendbruce
ascendbruce / gist:7070932
Last active December 26, 2015 01:19
Ruby performance/benchmark measure methods
# Ruby build-in
require 'benchmark'
puts Benchmark.measure { "a"*1_000_000 } # and Benchmark.bm, Benchmark.bmbm
# handcrafted
def performance
t1 = Time.now.to_f
yield
t2 = Time.now.to_f
return t2 - t1