Ruby Programming | 连载 01 - Intro and Setup

简介: Ruby Programming | 连载 01 - Intro and Setup

Section 1: Introduction to Ruby

What is Ruby?

  • Ruby is a object oriented programming language
  • Released in 1995 by Yukihiro Matsumoto ("Matz")
  • Open-source project in development today
  • Available for Linux、Windows and Mac OS operating systems
  • One of the top 10 popular languages worldwide

Why Ruby?

Ruby is EASY !

Ruby is known for it's simplicity and super easy relative to other programming languages. It's also you can see the slogan "A PROGRAMMER'S BEST FRIEND" in the Ruby offical website.

As it says the Ruby is design to make programming easy and simple and without forcing the programmer to learn a ton at the begging. It allows us dirve into it and learn a lot less before they are able to develop full scall programs.

image.png

Try run first line of Ruby code

3.times {print "Hello Ruby"}
复制代码

open the terminal and input irb, then you get into the Ruby command line ide.

image.png

Please ignore the irb that will introduce it later, just focus on the output message.

Design Principles

Ruby is a high-level programming language.

Ruby is a high level language, which means it's designed to be more readable by humans and it takes care of the work of taking our human readable code and then converting it to the lower lever code. The machine codes as it's called, that the computer can process.

Ruby is an interpreted language.

Ruby is an interpreted language just like python. It does not need to be compiled. the contrast or the comparsion to theat is a compiled language, and examples of compiled languages include Java and C++.

What this means is that Ruby's code is executed at runtime. So Whenever you run a file, that's when the Ruby interpretor, as we call it, runs throught the file.

Ruby code is read top-to-botton, line by line.

Top to bottom, line by line, much how you read a book or simply moves through the file that you have wirten and just runs throw the code line by line.

Ruby on Rails

The Ruby on Rails is a popular web development framework which build on top of the Ruby programming language. Ruby on Rails is an extension, It's an add on, It's an extra thing that's build on top of Ruby.

image.png

They are lots of startup company choose Ruby on Rails also called Rails as their web development framework like airbnb、GitHub、GitLab、Twitter....

image.png

Section 2: Install Ruby 2.7.6

The Ruby offical download page shows the way to install ruby in different machines.

When you run Ruby code on macOS machines, I strognly recommand you to install rvm. The rvm is a Ruby version manager tool, you can install or switch different Ruby version by using rvm command rvm install 2.3 and rvm use 2.7.6. But first you need to install rvm.

Here is the offical RVM website, the index page shows you the way of install rvm in you machines, It's very easy, just execute command in your terminal or command line.

\curl -sSL https://get.rvm.io | bash -s stable
复制代码

After successful install RVM, you can executed the below command to check the RVM version.

rvm -v
复制代码

Then you can check all suitable version of Ruby can be installed or other tools by RVM.

rvm list known
复制代码

image.png

# install Ruby 2.7.6
rvm install 2.7.6
# check Ruby veresion
ruby -v
复制代码

Below are the common commands that you may need.

# when you install serval versions of Ruby,you can switch Ruby version and set default version of system
rvm use 2.7.6 --default
# list installations by rvm
rvm list
# remove Ruby 2.7.6
rvm remove 2.7.6
复制代码

Section 3: Run Ruby File from Terminal and RubyMine

We gonna use RubyMine as the IDE and you can download it from Jebrains.

Open RubyMine and create a new project for Ruby code named "ruby-introduction".

image.png

Then you need to choose Ruby SDK, It's fine select the version installed last section.

image.png

Click Create button and it will be successful to create a Ruby project.

".rb" is a extension for Ruby file, so all Ruby files are ended with ".rb". It's a good idea to named you Ruby file with underscores instead of spaces when named by mutiple words.

So we can create a Ruby file named "hello_world.rb" and it will add ".rb" extension automatically when you select create Ruby file at RubyMine.

image.png

Enter below code into the "hello_world" Ruby file.

puts "Hello World!"
复制代码

Click "Run 'hello_world'".

image.png

Then you got text of "Hello World!" bottom of the RubyMine.

image.png

There is another way to run Ruby file, use command cd ruby-introduction enter the ruby-introduction folder and use command ruby hello_world.rb to execute Ruby file, and you will see the output of "Hello World" like below image.

image.png

Section 4: Interactive Ruby IRB

IRB functions as a sandbox or a plaground of Ruby, It's a place where you can go ahread and pratice or review you Ruby code withou any kind of consequences or anythings the could possible happen.

It's much easy to access IRB, just enter the terminal and type irb into the terminal,  it will show your ruby version and line number and you can write ruby code after the symbol ">" and press Enter you will get the result of ruby code after symbol "=>".

image.png


相关文章
|
Java C++ Ruby
Ruby Programming | 连载 06 - Ruby 面向对象
Ruby Programming | 连载 06 - Ruby 面向对象
Ruby Programming | 连载 06 - Ruby 面向对象
Ruby Programming | 连载 08 - Ruby 方法参数
Ruby Programming | 连载 08 - Ruby 方法参数
Ruby Programming | 连载 08 - Ruby 方法参数
Ruby Programming | 连载 07 - Ruby 对象的原生行为
Ruby Programming | 连载 07 - Ruby 对象的原生行为
Ruby Programming | 连载 07 - Ruby 对象的原生行为
|
存储 IDE 开发工具
Ruby Programming | 连载 03 - Ruby 对象基础
Ruby Programming | 连载 03 - Ruby 对象基础
Ruby Programming | 连载 03 - Ruby 对象基础
|
存储 Ruby
Ruby Programming | 连载 10 - Ruby 的类
Ruby Programming | 连载 10 - Ruby 的类
|
Scala Ruby
Ruby Programming | 连载 09 - Ruby 变量
Ruby Programming | 连载 09 - Ruby 变量
|
Linux 数据库 Ruby
Ruby Programming | 连载 05 - Ruby 工具集
Ruby Programming | 连载 05 - Ruby 工具集
|
存储 Java 数据挖掘
Ruby Programming | 连载 04 - Ruby 扩展基础
Ruby Programming | 连载 04 - Ruby 扩展基础
|
存储 对象存储 Ruby
Ruby Programming | 连载 02 - Ruby 语法基础
Ruby Programming | 连载 02 - Ruby 语法基础