Ruby Programming | 连载 01 - Intro and Setup

2022-09-26 21:12:24 浏览数 (2)

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第20天,点击查看活动详情

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.

Try run first line of Ruby code

代码语言:javascript复制
3.times {print "Hello Ruby"}

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

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.

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

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.

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

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

代码语言:javascript复制
rvm -v

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

代码语言:javascript复制
rvm list known
代码语言:javascript复制
# install Ruby 2.7.6
rvm install 2.7.6
# check Ruby veresion
ruby -v

Below are the common commands that you may need.

代码语言:javascript复制
# 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".

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

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.

Enter below code into the "hello_world" Ruby file.

代码语言:javascript复制
puts "Hello World!"

Click "Run 'hello_world'".

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

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.

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 "=>".

typeexitto exit IRB.

0 人点赞