Ruby basics: irb, puts and sockets

Introduction

Ruby is a dynamic, reflective, interpreted high-level programming language. It has an operating-system independent thread implementation, strong dynamic typing and a garbage collector, among many other features.

Syntactically it is close to Perl and Eiffel; its object-oriented approach comes from Smalltalk. Some language traits were also taken from Python, Lisp, Dylan and Clu.

The cross-platform Ruby interpreter is fully free software. To go deeper than this page, follow the Ruby course.

Interactive mode (irb)

To start the interactive shell run the command

irb

Output with puts

Output is done with the puts command

puts "HeiHei.ru"

To interpolate a variable you need # and {}

url = "HeiHei.ru" puts "Best website = #{url}"

Sockets

The socket library is not loaded by default, so require it first. To open a socket use the new or open method.

Suppose the port we want to create the connection on is 25110

require "socket" srv = TCPServer.new(25110)

Or

srv = TCPServer.open(25110)

Possible problem: you opened a socket this way. It listens on port 25110, but for some reason only on ipv6

And you need ipv4

In this case the code needs a small change

srv = TCPServer.new('0.0.0.0', 25110)

0.0.0.0 means that we want to connect over ipv4 and are ready to accept connections from any IP

Article author: Andrei Olegovich