Ruby for Rails 学习笔记(一)

来源:互联网 发布:新闻类app源码 编辑:程序博客网 时间:2024/05/02 02:22

0x00 Ruby的安装

apt (Debian or Ubuntu)

Debian GNU/Linux and Ubuntu use the apt package manager. You can use it like this:

$ sudo apt-get install ruby-full

As of writing, the ruby-full package provides Ruby 1.9.3, which is an old stable release, on Debian and Ubuntu.

Homebrew (OS X)

On OS X Yosemite and Mavericks, Ruby 2.0 is included. OS X Mountain Lion, Lion, and Snow Leopard ship with Ruby 1.8.7.

Many people on OS X use Homebrew as a package manager. It is really easy to get a newer version of Ruby using Homebrew:

$ brew install ruby

This should install the latest Ruby version.

安装完成后,查看Ruby的版本

$ ruby -v

0x01 Ruby基础知识:

puts和print

 print不带换行,如果要换行可以使用print "","\n"

文件输入和键盘输入

  • 文件输入 num = File.read(“XXX.dat”)
  • 键盘输入 num = gets

请求加载文件和加载文件

require 'XXX.rb' 查找另一个文件,并执行他,如果没有找到,程序将报致命错误。

load ‘XXX.rb’和require ‘XXX.rb’区别,

require 'XXX.rb'require 'XXX.rb'

第二次require什么都不会发生

load 'XXX.rb'load 'XXX.rb'

第二次会重新读入,同一个文件被读入两次,看起来好像没有什么意思,但是实际上,这个对于Rails框架来说,这个特性非常重要。

命令行开关

  • 语法检查 -c
  • 打开警告 -w
  • 查看版本 -v

交互式解释器 irb

其他工具

  • 用于生成ri(索引)和 RDoc(Ruby文档)
  • 将Ruby嵌入到Html中 ERb
<% title =  "hello" %><% test = "test" %><html><head><title><%= title %> </title></head><body><%= test %></body></html>

以上代码保存为test.rb
运行erb test.rb
输出:

<html><head><title>hello </title></head><body>test</body></html>

这部分是Rails的重要特性。

git hub:
git@github.com:xuqi1987/ruby-on-rails.git

参考链接:
https://www.ruby-lang.org/en/downloads/

0 0
原创粉丝点击