DevHeads.net

Postings by Peter Hickman

Detecting lost connections in TCPServer connection

I have some code similar to the following:

#!/usr/bin/env ruby

require 'socket'

server = TCPServer.new('localhost', 2000)

loop do
Thread.start(server.accept) do |client|
puts 'Client connects'

loop do
s = client.gets
break if s.index('quit') == 0
client.puts "Hello #{s}"
end

puts 'Client disconnects'

client.close
end
end

When this runs a new connection will spawn a new thread.

Hacked

Some of my servers have been hacked and running a monero(?) coin miner. It
creates a directory
called /tmp/systemd-private-60ffef34724f43b19fa2d3962b83687e-systemd-timesyncd.service-sPMHHT
(or similar)

Also at the same time a /tmp/bundle directory is created

Do these seem related? Does anyone have an idea on this?

Behaviour of round changed?

I am moving some algorithms between 1.9.3 and 2.4.2 and found (after much
head scratching)

2.4.2 :001 > 1.015.round(2)
=> 1.02

1.9.3-p551 :001 > 1.015.round(2)
=> 1.01

We are calculating margin prices and round input values to 2 places because
the precision is not needed and it can help with caching

The tests broke :(

Let me see if I understand this, Ruby has changed how basic maths functions
work

Can I trust ceil, floor, truncate or any of the other number related
functions?

Do I need to write tests for the language itself for when I upgrade to a
newer version in the future or should

Threads, Sockets and OSX

I have a little app that is basically the standard threaded TCPServer with
a couple of mutexes to implement a fifo queue. When it runs it goes flat
out for 15-20 seconds and then freezes for 10-15 seconds.

Rapid array accesses and garbage collection

I have a small app that uses an Array as a queue. Works well enough but
(seems) to suffer from GC lockouts

Items are added and removed (<< and shift) from the Array frequently and
the queue itself rarely gets very large, 10s to 100s of records, but the
throughput is 100,000s of records

Which is, I assume, causing it to periodically freeze. The question is:

1) Should I manually start GC after every shift operation. Will this trade
off frequent small GC interruptions for less big freezes
2) Should I manually start GC after X shifts.

Shell in old versions of NetSSH

In older versions of net-ssh there used to be a shell function that allowed
you to open up a shell on the remote host and issue commands. At some point
this was removed

Presently it is possible to issue commands with something like

ssh = Net::SSH.start(...)
ssh.exec!(some command or other)

The problem is each exec! call opens a new shell on each call so it is not
possible to retain your state on the remote machine.

Method too long problem

I am converting a spreadsheet into ruby so that it may be executed as part
of a bigger system. This has worked well for sheets with only 1,000s of
cells. There is a problem with sheets that have nearly 200,000 cells.

Patching Float within a scope

I have the need for things like nil + 12.0 to return nil in the way that
some software packages propagate NaN and other oddities.

In my code I have this sort of thing which solves the problem.

class Float
alias old_plus +
def +(other); other ? old_plus(other) : other; end
end

class NilClass
def +(_other); self; end
end

Except that the patch is now permanent and will affect all parts of the
application.

Getting gems to deploy correctly

I know this might be more of a Rails question but I have a problem with
installing some gems that I have created.

In my Gemfile I have

gem 'fred-gem', '1.3.0', :path => '../fred-gem'

In my Gemfile.lock this becomes:

PATH
remote: vendor/cache/fred-gem
specs:
fred-gem (1.3.0)
rails (~> 3.1.0)

In my vendor/cache I have a directory called fred-gem/ with the a copy of
the ../fred-gem directory in it.

However when I deploy it with 'bundle exec cap production deploy' I get

** [out :: example.com] You are trying to install in deployment mode after
changing
** [out :: example.com]

Daily Reminder: TCL > Ruby 9800

shoutz to irc.gangnet.ru #tclmafia 1490

Daily Reminder: TCL > Ruby 28179

shoutz to irc.gangnet.ru #tclmafia 17968

Daily Reminder: TCL > Ruby 2853

shoutz to irc.gangnet.ru #tclmafia 29804

Daily Reminder: TCL > Ruby 2008

shoutz to irc.gangnet.ru #tclmafia 14912

Daily Reminder: TCL > Ruby 23155

shoutz to irc.gangnet.ru #tclmafia 32267

Certificates and Net::HTTP

I am in the process of converting some Python code over to Ruby.

Cometd client for Ruby

I am going to have to connect to a comet server using Ruby. What I need is
something that can be used directly from a Ruby program that has absolutely
nothing to do with Rails.

rayeux is no longer supported and the last commit was 2 years ago
faye (the replacement given on the rayeux page) is a 404 on github

All the links that Google kicks up seem to be completely obsessed with
using Javascript in Rails applications or want me to set up a node.js
environment for some reason.

New Rexx like data structure

This is just something that I have been playing with for some time but I
thought a structure like this might be cool.

a = Hash.new
a.b = 12

This behaves the same as a['b'] = 12, and this ...

a.c.d = 42

behaves like this ...

a['c']['d'] = 42

The syntax looks cleaner to me if a little Rexxish (or Lua like).

Playing with DSLs, a question or two

I am playing around with writing some DSLs to help me get more familier
with how they work and I have a couple of questions.

Splitting a string into characters - not bytes

I realise that this is probably a known thing but my google-fu is not
working today.

I am learning Japanese and I wanted to write an application (probably
a web app) that I can paste some Japanese into and highlight the
characters that I should know. To do this I need to take a string of
Japanese, such as "目が覚めてしまった。眠いのに…。

Odd functional programming question

Ok this is probably not really functional programming but I was just
noodling about with Ruby when I thought "it would be quite nice if I
could do X". In this case X is as follows:

def X(text, action)
x.action
end

Quite clearly this does not actually work, otherwise I wouldn't be
asking. But is there some way in Ruby to do something like this?