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. Cygwin使用1-root用户登录

    设置cygwin中的root用户登录 在windows中模拟linux环境,可以安装cygwin.cygwin安装之后,系统默认的是以你的windows用户名做为cygwin的登录名的.权限却依然是普 ...

  2. GetPostBackEventReference加RaisePostBackEvent实现自定义控件中回调传参

    ; //回调函数,回调参数值:eventArgument        public void RaisePostBackEvent(string eventArgument)        {    ...

  3. 51nod 1132 覆盖数字的数量 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1132 题意是给定a,b,l,r求[l,r]内有几个整数可以表示成ax+b ...

  4. 峰Redis学习(4)Redis 数据结构(List的操作)

    第四节:Redis 数据结构之List 类型 存储list: ArrayList使用数组方式 LinkedList使用双向链接方式   双向链接表中增加数据 双向链接表中删除数据   存储list常用 ...

  5. 服务网关zuul之四:zuul网关配置

    禁用过滤器在Zuul中特别提供了一个参数来禁用指定的过滤器,该参数的配置格式如下:zuul.AccessFilter.pre.disable=true动态加载动态路由通过结合Spring Cloud ...

  6. Java学习——用户电话输入显示

    编写程序:在窗口中添加组件,产生如下图形化界面:当用户输入用户名和电话后,点击显示按钮后,按下图格式显示. package cys; import java.awt.*; import java.aw ...

  7. 【ZZ】详解哈希表的查找

    详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...

  8. jquery插件:textarea的字数提示、textBox的文字提示

    引用文件:    <script src=”/TextTip/TextTip.js” type=”text/javascript”></script> 一.textarea的字 ...

  9. Ajax的课外了解

    Ajax传入的数据的话,只能是字符串或数字,字段,其他形式的传参都不可以: Ajax只是跟后台交互也有同源策略的限制: 不是当前服务器叫跨域: Ajax也有同源策略的限制想做跨域处理,只能通过scri ...

  10. folly无锁队列正确性说明

    folly无锁队列是facebook开源的一个无所队列,使用的是单向链表,通过compare_exchange语句实现的多生产多消费的队列,我曾经花了比较多的时间学习memory_order的说明,对 ...