Ruby 有4种数据类型:String, Boolen, Array, Hashes

Ruby 有3种操作方法:Method, attribute, ??

Ruby 有xxx: Classes, Object....

====先来看数据类型====

1. String and Declaring the variables:

name = "Wonder Woman"   #declare a var and store a string
puts name <span style="white-space:pre"> </span>#puts -- print out the var sum = 5 + 1.4
puts sum correct = 1 == 1
puts correct

2. Arrays:

cities = ["chongqing","beijing","shanghai"]

puts cities[1];		#print out the SECOND city

3.Hashes:

注意这句话:We can access any value by naming its key

seasons = { "Spring" => 20, "Summer"=>30, "Autumn"=>20, "Winter"=>02}
puts seasons["Winter"] #{ ? , ? , ? }
# "key"
# "key" => value
# access: HashName[ "key" ] = value

4. Declare and Refer the variables

foods = ["apple", "pear", "orange"]
puts "my favourite foods are #{foods}"
# here we use #{} to refer to the variables.

5. Methods

For all object types, Ruby has a number of built in methods that allow us to change the object. Let's look at a few common ones:

a. Strings:     .reverse, .capitalize

b. Numbers: + , - , * , /

c. Arrays:      .delete, .count

d. Hashes:    .compare, .flatten

用.号来调用methods.

colors = ["oo", "tt" , "tt", "ff"]
puts colors.first # call the method .first on array

6. Define our own methods

def clock(time)
puts "It's #{time}!"
end
clock("10:00pm") #note the ""

7. if ... else ... end

num = 6
if num.even?
puts "This int is even."
else
puts "This int is odd."
end # don't forget the end

8. Iterator - 迭代器

for Array and Hash ,使用迭代器来遍历Access each element.

names = ["Tommy","Catty","Barry","Sunny"]
names.each do |nname|
puts "hello #{nname}!"
end

9. Classes - 类

关于Class的声明和使用:

class Person

 def hello
puts "hello"
end end person1 = Person.new
person1.hello person2 = Person.new
person2.hello

another e.g.

class Person

  def initialize(name, age)
@name = name
@age = age
end def intro
puts "My name is #{@name} and I am #{@age} years old"
end end person1 = Person.new("Lupe", 8)
person1.intro class Dog
def initialize(name,color)
@name = name
@color= color
end
def describe
puts "My name is #{@name} and I am #{@color}"
end
end
dog1 = Dog.new("Rover","beige")
dog1.describe

Ruby学习笔记1 -- 基本语法和数据类型, Class的更多相关文章

  1. js学习笔记1:语法、数据类型与转换、运算符与运算

    注意: 上部代码错误,将停止运行,下部的代码无法显示            typeof 用来定义内容类型,不会输出内容只会输出类型 一.js输出语法         1. 弹窗输出('')内的内容: ...

  2. JavaScript学习笔记(5)——JavaScript语法之数据类型

    JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: var x // x 为 undefined var x = 6; // x 为数字 var x = "Bill&q ...

  3. JavaScript:学习笔记(2)——基本概念与数据类型

    JavaScript:学习笔记(2)——基本概念与数据类型 语法 1.区分大小写.Test 和 test 是完全不同的两个变量. 2.语句最好以分号结束,也就是说不以分号结束也可以. 变量 1.JS的 ...

  4. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  5. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  6. Java学习笔记之---基础语法

    Java学习笔记之---基础语法 一. Java中的命名规范 (一)包名 由多个单词组成时,所有字母小写(例如:onetwo) (二)类名和接口 由多个单词组成时,所有单词首字母大写(例如:OneTw ...

  7. opencv学习笔记(三)基本数据类型

    opencv学习笔记(三)基本数据类型 类:DataType 将C++数据类型转换为对应的opencv数据类型 OpenCV原始数据类型的特征模版.OpenCV的原始数据类型包括unsigned ch ...

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

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

  9. python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样

    python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入 ...

随机推荐

  1. 安装tensorflow,那叫一个坑啊

    最近,项目团队需要研究并应用AI的技术,在具体的产品实施环节中使用.之前的几个项目,是委托武汉大学给做的,基于keras框架,实现了一些图像识别的项目. 这不,上方希望自己能够掌握一些常用且成熟的AI ...

  2. Team Foundation Server 2010简体中文版

    文件名称:Team Foundation Server 2010简体中文版 文件大小:1.8 GBhttp://www.google.com/profiles/dedecms.com 下载地址: th ...

  3. console call的fallback console 兼容

    (function() { var noop = function noop() {}; var methods = [ 'assert', 'clear', 'count', 'debug', 'd ...

  4. ALGO-9_蓝桥杯_算法训练_摆动序列(DP)

    问题描述 如果一个序列满足下面的性质,我们就将它称为摆动序列: . 序列中的所有数都是不大于k的正整数: . 序列中至少有两个数. . 序列中的数两两不相等: . 如果第i – 1个数比第i – 2个 ...

  5. autoconf配置的项目,编译debug版本

    ./configure CFLAGS=" -g " 当然,c++代码就把 CFALGS 改成 CPPFLAGS

  6. Javascript之类型转换(二)

    前言 类型转换js中主要有以下几种情况: 1.条件判断时: 2.对象转基本类型时: 3.四则运算时: 4.‘==’操作符比较值时: 5.比较运算符时. 一.条件判断时 在条件判断时,除了 undefi ...

  7. ActionScript3.0实现动态地图效果

    14年的一个项目需求,研究了一下AS脚本.AS2.0是之前面向关系的语言,AS3.0之后开始走上面向对象路线. 现在附上当时的代码,里边包含很多细节和算法,重要的代码也都有注释,如果需要可以仔细看一下 ...

  8. PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  9. 廖雪峰Java3异常处理-1错误处理-1Java的异常

    1.计算机运行中的错误 在计算机程序运行的过程中,错误总会出现,不可避免的 用户输入错误 读写文件错误 网络错误.内存耗尽.无法连接打印机不可 String s = "abc"; ...

  10. Windows和pthread中提供的自旋锁

    Windows和POSIX中都提供了自旋锁,我们也可以通过C++11的atomic来实现自旋锁.那么两者性能上面是什么关系?先引入实现代码: #ifndef __spinlock_h__ #defin ...