Ruby basics: irb, puts and sockets

Contents
Introduction
Interactive mode (irb)
Output with puts
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: zero to hero in 50 lessons.

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

To open a socket use the new or open method.

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

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

Related articles
Ruby
Ruby basics: irb, puts and sockets
Ruby course: zero to hero in 50 lessons
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: