Monday, May 13, 2019

Detecting key press in Ruby

To check whether a key is pressed in linux (non-blocking, non-waiting).
def keypressed
  system('stty raw -echo') # => Raw mode, no echo
  char = (STDIN.read_nonblock(1).chr rescue nil)
  system('stty -raw echo') # => Reset terminal mode
  char
end
Inspired on https://stackoverflow.com/a/22659929.

Saturday, October 27, 2018

Net::HTTP error sending GET request with parameters

If you're getting trouble with Net::HTTP#send_request when sending a GET request with parameters, one possible cause is that parameters must be part of path as a query string, and not as data (3rd parameter of #send_request).

For example (assuming that parameters is not empty), instead of

http = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)

try:

http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)

Net::HTTP error 400 (bad request) with HTTPS

Hello, friends!

Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:

      http = Net::HTTP.new('server.com', 443)
      response = http.send_request('GET', path, parameters, headers)

I was getting error 400 in response.code.

What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:

      http = Net::HTTP.new('server.com', 443)
      http.use_ssl = true
      response = http.send_request('GET', path, parameters, headers)


Tuesday, March 21, 2017

Pipe inside a bash script

Hello!

Today I got stuck trying to use pipe inside a command variable in a bash script:

$CMD="echo foo | cat"
EXEC=`$CMD`                 # expecting foo
echo $EXEC                  # got foo | cat


I could solve that if I run the command directly, without variables:

EXEC=`echo foo | cat`       # expecting foo
echo $EXEC                  # got foo


But that was not an option, because $CMD was complex and carefully mounted before that snippet.

Googling around I found that the solution is to use eval (although I used it slightly different from the source):

$CMD="echo foo | cat"
EXEC=`eval $CMD`            # expecting foo
echo $EXEC                  # got foo


The reason is that eval can expand the pipe command inside a variable, while backticks can't.

Cheers!

Wednesday, August 31, 2016

How to install mysql2 gem on Ubuntu

If you are having trouble to install the mysql2 gem, try the following:

# apt-get install build-essential ruby-dev libmysqlclient-dev

Sunday, July 24, 2016

Slides: Introduction to Ruby

I'm here today to share a presentation that I've made to a group of Software Development students.

Click here to see the presentation.

I'm sorry for a piece of code that was not translated (variable names in portuguese), but I think that you can get the idea (comparing how PHP is spelling and how Ruby is expressive).

Monday, April 21, 2014

Installing Ruby 2.1.1 in Ubuntu 14.04

The apt-get way don't install the latest stable Ruby (2.1.1), even on Ubuntu 14.04. Instead, I got 1.9.3 that way. RVM can be easy, but to work with Apache I'd need a system-wide installation independent of the RVM environment. So I've decided to install Ruby from source. I had problems around readline, but after seeing this hint, the solution that worked for me is:

./configure --with-readline-dir=/usr/lib/x86_64-linux-gnu/libreadline.so
make
make install

on the directory of the unpacked source of Ruby 2.1.1, as root. I suspect there is a more elegant solution out there, but this is enough for me at this time.