DRY Ruby Initialization with Hash Argument

You don’t need the constant, but I don’t think you can eliminate symbol-to-string: class Example attr_reader :name, :age def initialize args args.each do |k,v| instance_variable_set(“@#{k}”, v) unless v.nil? end end end #=> nil e1 = Example.new :name => ‘foo’, :age => 33 #=> #<Example:0x3f9a1c @name=”foo”, @age=33> e2 = Example.new :name => ‘bar’ #=> #<Example:0x3eb15c @name=”bar”> … Read more

How do you downgrade rubygems?

This worked for me when downgrading from 1.5.2 to 1.4.2 because of old rails version: sudo gem update –system 1.4.2 More information about downgrading/upgrading rubygems: https://github.com/rubygems/rubygems/blob/master/UPGRADING.md

Serving static files with Sinatra

You can use the send_file helper to serve files. require ‘sinatra’ get “https://stackoverflow.com/” do send_file File.join(settings.public_folder, ‘index.html’) end This will serve index.html from whatever directory has been configured as having your application’s static files.

How do I parse a YAML file in Ruby?

Maybe I’m missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result? If your sample YAML is in some.yml, then this: require ‘yaml’ thing = YAML.load_file(‘some.yml’) puts thing.inspect gives me {“javascripts”=>[{“fo_global”=>[“lazyload-min”, “holla-min”]}]}

How to avoid “cannot load such file — utils/popen” from homebrew on OSX

Original Answer The problem mainly occurs after updating OS X to El Capitan (OS X 10.11) or macOS Sierra (macOS 10.12). This is because of file permission issues with El Capitan’s or later macOS’s new SIP process. Try changing the permissions for the /usr/local directory: $ sudo chown -R $(whoami):admin /usr/local If it still doesn’t … Read more