Ruby Programming | 连载 01 - Intro and Setup
持续创作,加速成长!这是我参与「掘金日新计划 · 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
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.
\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
# 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".
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.
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 "=>".
typeexit
to exit IRB.
相关文章
- 【Ruby高级技术】对面向对象里的控制访问,包括类的继承类常量的深入理解和使用
- Ruby 时间函数详解编程语言
- MySQL中的OR与AND操作符比较(mysqlor和and)
- 如何在CentOS 8上安装Ruby on Rails
- 「Linux上安装Ruby的指南」(linux安装ruby)
- 浅谈ruby core library 与 Liskov Substitution Principle原则
- Ruby中循环引用的检查方法
- Ruby中循环引用导致lighttpd中的ruby fcgi脚本执行失败(Mac OS)
- 深入浅出:MySQL中AND和OR运算符使用方法(mysql中and和or)
- 条件MySQL 子句之间`AND`操作符多条件查询(mysql多个and)
- Oracle数据库中的AND查询(oracle and查询)
- MySQL中AND的使用方法解析(mysql中and的用法)
- Ruby快速操控Redis实现数据存储(ruby 操作redis)
- 里的应用Ruby语言与Redis结合应用的一次尝试(ruby在redis)
- 两种技术的融合借助 Ruby 与 Redis 实现强大的数据处理(ruby和redis)
- 部署用Ruby和Redis远程部署配置生产环境(ruby redis远程)
- 利用Redis集群搭建Ruby缓存系统(redis集群 ruby)
- ruby存取器概念
- ruby程序的执行顺序
- ruby、javascript、php中的观察者模式实现代码
- Ruby中字符串左侧补零方法实例
- Ruby类继承、抽象类、类拓展混入、代理类实例
- Ruby简洁学习笔记(一):字符串、数字、类和对象
- Ruby中的Mechanize的使用教程