How to pass a custom comparator to “sort”?

Define your own <=>, and include Comparable. This is from the Comparable doc: class SizeMatters include Comparable attr :str def <=>(an_other) str.size <=> an_other.str.size end def initialize(str) @str = str end def inspect @str end end s1 = SizeMatters.new(“Z”) s2 = SizeMatters.new(“YY”) s3 = SizeMatters.new(“XXX”) s4 = SizeMatters.new(“WWWW”) s5 = SizeMatters.new(“VVVVV”) s1 < s2 #=> … Read more

Get child constant in parent method – Ruby

EDIT: this answer is correct, although Wayne’s is the more ruby-ish way to approach the problem. Yes it is. Your implementation will not work, because the parent tries to resolve EWOK locally. Parent doesn’t have EWOK defined. However, you can tell Ruby to look specifically at the class of the actual instance the method was … Read more

Couldn’t require openssl in ruby

Note: Calls to rubygems.org are deprecated – proceed with caution! I had the same issue on Mac OSX after also building ruby2.1.0p0 from the source. I already had openssl installed. It appears that the reference in gems needed refreshing. I ran: gem source -r https://rubygems.org/ to remove followed by gem source -a https://rubygems.org/ to read … Read more

How do I include variables in my VagrantFile?

I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and… In my Vagrantfile: # encoding: utf-8 # -*- mode: ruby -*- # vi: set ft=ruby : require ‘yaml’ current_dir = File.dirname(File.expand_path(__FILE__)) configs = YAML.load_file(“#{current_dir}/config.yaml”) vagrant_config = configs[‘configs’][configs[‘configs’][‘use’]] Vagrant.configure(‘2’) do |config| config.vm.network ‘public_network’, ip: vagrant_config[‘public_ip’] … In … Read more

Regex with named capture groups getting all matches in Ruby

Named captures are suitable only for one matching result. Ruby’s analogue of findall is String#scan. You can either use scan result as an array, or pass a block to it: irb> s = “123–abc,123–abc,123–abc” => “123–abc,123–abc,123–abc” irb> s.scan(/(\d*)–([a-z]*)/) => [[“123”, “abc”], [“123”, “abc”], [“123”, “abc”]] irb> s.scan(/(\d*)–([a-z]*)/) do |number, chars| irb* p [number,chars] irb> end … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)