环境搭建

本文基于Mac OS,windowns坑较多,建议使用Mac。

  • xcode-select -p 检查是否安装xcode-select, 如果没有,通过xcode-select --install命令进行安装,ruby安装时需要这个library。
  • /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ,安装Homebrew包管理工具,类似于npm, 官网地址
  • brew install git 安装Git
  • \curl -L https://get.rvm.io | bash -s stable 安装RVM (Ruby Version Manager)
  • rvm install 2.3.1 安装ruby,2.3.1为版本号

    rvm list 列出已经安装ruby版本,rvm use 2.3.1 --default 设定默认的ruby版本。
  • gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/ 由于rubygems被墙,可以添加淘宝提供的Gem下载站点。
  • 编辑器使用Atom,IDE可以使用RubyMine。

类/对象

  • ruby中几乎所有见到的都是对象。
  • 和Java一样,ruby通过关键字class定义一个类。
class Dog
def bark
puts "wang wang..."
end
def self.static_method
puts "this is a static method"
end
end

通过 Dog.new.bark调用实例方法,Dog.static_method调用类方法。

  • 类变量
class House
@@location = 'Xi\'an'
def self.location
@@location
end
end
puts House.location => Xi'an

通过关键字'<'表示继承.

class Apartment < House
@@location = 'Gaoxin'
end
puts House.location => Gaoxin
puts Apartment.location => Gaoxin

修改类变量会修改对应的父类和所有子类。

  • 实例变量
class House
attr_accessor :location
@location = 'xi\'an'
end
puts House.new.location => nil

实例变量变为类变量

class House
class << self
attr_accessor :location
end
@location = 'Xi\'an'
end
class Apartment < House
@location = 'Gaoxin'
end
puts House.location => Xi'an
puts Apartment.location => Gaoxin

Ruby 学习笔记(一)的更多相关文章

  1. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  2. ruby学习笔记(1)-puts,p,print的区别

    ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...

  3. ruby 学习笔记 1

    写ruby blog  系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...

  4. ruby学习笔记(2)-chomp,chop的区别

    还没开始系统性的学习Ruby,最近在看metasploit框架的exploit会涉及到Ruby脚本,也就硬着头皮一遍查阅资料一遍做些笔记吧. Ruby字符串中存在chop和chomp的内置函数.我在h ...

  5. Ruby学习笔记0708

    #!/usr/bin/env ruby class MegaGreeter attr_accessor :names # 初始化這個物件 def initialize(names = "Wo ...

  6. ruby 学习笔记 2 -变量

    变量 在ruby的世界里,变量有5种,全局变量 局部变量 实例变量 常量 类变量以及伪变量 常用的: 全局: 在全局使用,使用$开头,因为是全局的,所以在任何的代码例子中都可以改变其值,造成混乱,所以 ...

  7. Ruby学习笔记

    #!/usr/bin/ruby puts "Hello, Ruby, what is your name?" $name = STDIN.gets puts "Hi, I ...

  8. Ruby学习笔记(二)

    1.block 代码块 do...end 或 {} 构成一个代码块,就像常见的 .each后面跟的代码块. my_nums = [1,2,3] my_double_nums = my_nums.col ...

  9. Ruby学习笔记之升级ruby的版本

    升级ruby版本,有时候安装ruby的版本过低,需要进行升级,例如安装在centos6.7安装fpm需要ruby版本在1.9以上. 0x00 主机环境如下 [root@test ~]# cat /et ...

  10. Ruby学习笔记7: 添加身份验证(adding Authentication)

    我们已经完成了Category & Product页面内容的增删改查,再加入一个身份验证即可成为一个较完整的Rails App了.本文就来完成这个任务. We now need to give ...

随机推荐

  1. Unity Shader着色器优化

    https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247493518&idx=1&sn=c51b92e9300bcf ...

  2. CentOS系统日志

    日志分类: 一./var目录 一些数据库如MySQL则在/var/lib下,用户未读的邮件的默认存放地点为/var/spool/mail 二.:/var/log/ 系统的引导日志:/var/log/b ...

  3. springboot 简单使用shiro登录

    首先引入需要的pom <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shir ...

  4. EcmaScript内置对象的属性与方法

  5. Knight Tournament (set)

    Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...

  6. 瓷砖覆盖(状压DP)

    题目描述 Description 用1*2的瓷砖去铺N*M的地面,问有多少种铺法 输入描述 Input Description 第一行有两数n,m.表示地面的大小 输出描述 Output Descri ...

  7. Django组件-cookie,session

    昨日内容回顾: json 轻量级的数据交换格式 在python 序列化方法:json.dumps() 反序列化方法:json.loads() 在JS中: 序列化方法:JSON.stringfy() 反 ...

  8. C++学习 - 虚表,虚函数,虚函数表指针学习笔记

    http://blog.csdn.net/alps1992/article/details/45052403 虚函数 虚函数就是用virtual来修饰的函数.虚函数是实现C++多态的基础. 虚表 每个 ...

  9. ASP.NET相关

    1.委托:把一个方法当作参数传到另一个方法中 扩展方法:1.静态类 2.静态方法 3.this关键字 using System; using System.Collections.Generic; u ...

  10. 对象拷贝 - 优雅的解决方案 Mapstruct

    MapStruct GitHub 访问地址 : https://github.com/mapstruct/mapstruct/ 使用例子 : https://github.com/mapstruct/ ...