Best practices with STDIN in Ruby? [closed]

Following are some things I found in my collection of obscure Ruby. So, in Ruby, a simple no-bells implementation of the Unix command cat would be: #!/usr/bin/env ruby puts ARGF.read ARGF is your friend when it comes to input; it is a virtual file that gets all input from named files or all from STDIN. … Read more

How to sort an array in descending order in Ruby

It’s always enlightening to do a benchmark on the various suggested answers. Here’s what I found out: #!/usr/bin/ruby require ‘benchmark’ ary = [] 1000.times { ary << {:bar => rand(1000)} } n = 500 Benchmark.bm(20) do |x| x.report(“sort”) { n.times { ary.sort{ |a,b| b[:bar] <=> a[:bar] } } } x.report(“sort reverse”) { n.times { ary.sort{ … Read more

What is the difference between require_relative and require in Ruby?

Just look at the docs: require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement. For example, if you have unit test classes in the “test” directory, and data for them under the test “test/data” directory, then you might use a line … Read more

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

Replace words in a string – Ruby

sentence.sub! ‘Robert’, ‘Joe’ Won’t cause an exception if the replaced word isn’t in the sentence (the []= variant will). How to replace all instances? The above replaces only the first instance of “Robert”. To replace all instances use gsub/gsub! (ie. “global substitution”): sentence.gsub! ‘Robert’, ‘Joe’ The above will replace all instances of Robert with Joe.

Ruby ampersand colon shortcut [duplicate]

Your question is wrong, so to speak. What’s happening here isn’t “ampersand and colon”, it’s “ampersand and object”. The colon in this case is for the symbol. So, there’s & and there’s :foo. The & calls to_proc on the object, and passes it as a block to the method. In Ruby, to_proc is implemented on … Read more

When to use lambda, when to use Proc.new?

Another important but subtle difference between procs created with lambda and procs created with Proc.new is how they handle the return statement: In a lambda-created proc, the return statement returns only from the proc itself In a Proc.new-created proc, the return statement is a little more surprising: it returns control not just from the proc, … Read more

Getting output of system() calls in Ruby

I’d like to expand & clarify chaos’s answer a bit. If you surround your command with backticks, then you don’t need to (explicitly) call system() at all. The backticks execute the command and return the output as a string. You can then assign the value to a variable like so: output = `ls` p output … Read more

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