루비 프로그래밍 언어 스터디 노트 #1

Ruby Programming Language Study Note

This study note is from Tutorialspoint(https://www.tutorialspoint.com/ruby/).
I merely copied the contents down by typing, as I use this tutorial to study the Ruby programming language.
이 스터디 노트는 제가 그냥 Tutorialspoint 강좌를 보고 학습목적으로 따라친 것입니다.
출처는 Tutorialspoint(https://www.tutorialspoint.com/ruby/)입니다.

Introduction

Ruby is a scripting language designed by Yukihiro Matsumoto,
also known as Matz.

Ruby is a pure object-oriented programming language.
It was created in 1993 by Yukihiro Matsumoto of Japan.

Ruby is "A Programmer’s Best Friend".

Ruby has features that are similar to those of Smalltalk, Perl, and Python.
Perl, Python, and Smalltalk are scripting languages.

Smalltalk is a true object-oriented language.
Ruby, like Smalltalk, is a perfect object-oriented language.
Using Ruby syntax is much easier than using Smalltalk syntax.

Features of Ruby

  • Ruby is an open-source and is freely available on the Web,
    but it is subject to a license.
  • Ruby is a general-purpose, interpreted programmng language.
  • Ruby is a true object-oriented programming language.
  • Ruby is a server-side scripting langauge similar to Python and Perl.
  • Ruby can be used to write Common Gateway Interface(CGI) scripts.
  • Ruby can be embedded into HTML.
  • Ruby has a clean and easy syntax that allows a new developer
    to learn very quickly and easily.
  • Ruby has similar syntax to that of many programming languages
    such as C++ and Perl.
  • Ruby is very much scalable and big programs written in Ruby
    are easily maintainable.
  • Ruby can be used for developing Internet and intranet applications.
  • Ruby can be installed in Windows and POSIX environments.
  • Ruby support many GUI tools such as Tcl/Tk, GTK, and OpenGL.
  • Ruby can easily be connected to DB2, MySQL, Oracle, and Sybase.
  • Ruby has a rich set of built-in functions,
    which can be used directly into Ruby scripts.

Tools I Will Need

  • Linux
  • Apache 1.3.19-5 Web server
  • a modern Web browser
  • Ruby

This tutorial will provide the necessary skills to create GUI, networking,
and Web applications using Ruby. It also will talk about extending and
embedding Ruby applications.

Local Environment Setup

If you are still willing to set up your environment for Ruby programming
language, then let’s proceed. This tutorial will teach you all the
important topics related to environment setup. We would recommend you to
go through the following topics first and then proceed further —

  • Ruby Installation on Linux/Unix

Popular Ruby Editors

To write your Ruby programs, you will need an editor —

  • VIM is a very simple text editor. This is available on almost all
    machines.

Interactive Ruby(IRb)

Interactive Ruby(IRb) provides a shell for experimentation. Within the IRb
shell, you can immediately view expression results, line by line.

This tool comes along with Ruby installation so you have nothing to do
extra to have IRb working.

Just type irb at your command prompt and an Interactive Ruby Session
will start.

A Simple Program in Ruby

Let us write a simple program in Ruby. All Ruby files will have extension
.rb. So, put the following source code in a test.rb file.

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

You have seen a simple Ruby program, now let us see a few basic concepts
related to Ruby syntax.

Whitespace in Ruby Program

Whitespace characters such as spaces and tabs are generally ignored in
Ruby code, except when they appear in strings. Sometimes, however, they
are used to interpret ambiguous statements. Interpretations of this sort
produce warnings when the -w option is enabled.

Line Endings in Ruby Program

Ruby interprets semicolons and newline characters as the ending of a state-
ment. However, if Ruby encounters operators, such as +, - or \ at
the end of a line, they indicate the continuation of a statement.

Ruby Identifiers

Identifiers are names of variables, constants, and methods. Ruby
identifiers are case-sensitive.

Ruby indentifier names may consist of alphanumeric characters and the
underscore character(_).

Heredoc in Ruby

"Here Document" refers to building strings from multiple lines. Following
a <<, you can specify a string or an identifier to terminate the string
literal, and all lines following the current line up to the terminator are
the value of the string.

If the terminator is quoted, the type of quotes determines the type of the
line-oriented string literal. Notice there must be no space between <<
and the terminator.

Here is a set of example codes.

#!/usr/bin/ruby -w

print <<EOF
    This is the first way of creating 
    heredoc i.e. multiple line string.
EOF

print <<"EOF"; # same as above
    This is the second way of creating
    heredoc i.e. multiple line string.
EOF

print <<EOC # execute commands
    echo hi there
    echo lo there
EOC

print <<"foo", <<"bar" # you can stack them
    I said foo.
foo
    I said bar.
bar

Ruby BEGIN Statement

Syntax

BEGIN {
code
}
Declares code to be called before the program is run.

Example

#!/usr/bin/ruby

puts "This is the main Ruby program"

BEGIN {
    puts "Initializing Ruby program"
}

Ruby END Statement

Syntax

END {
    code
}

Declares code to be called at the end of the program.

Example

#!/usr/bin/ruby

puts "This is main Ruby program"

END {
    puts "Terminating Ruby program"
}
BEGIN {
    puts "Initializing Ruby program"
}

Ruby Comments

A comment hides a line, part of a line, or several lines from the Ruby
interpreter. You can use the hash character(#) at the beginning of a
line–

# I am a comment. Just ignore me.

Or, a comment may be on the same line after a statement or expression–

name = "Madisetti" # This is another comment

You can comment multiple lines as follows–

# This is a comment.
# This is a comment too.
# This is another comment.
# I said that already.

Here is another form. This block comment conceals several liens from the
interpreter with =begin/=end.–

=begin
This is a comment.
This is a comment too.
This is another comment.
I said that already.
=end

Leave a Reply