Ruby学习笔记1 -- 基本语法和数据类型, Class
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的更多相关文章
- js学习笔记1:语法、数据类型与转换、运算符与运算
注意: 上部代码错误,将停止运行,下部的代码无法显示 typeof 用来定义内容类型,不会输出内容只会输出类型 一.js输出语法 1. 弹窗输出('')内的内容: ...
- JavaScript学习笔记(5)——JavaScript语法之数据类型
JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: var x // x 为 undefined var x = 6; // x 为数字 var x = "Bill&q ...
- JavaScript:学习笔记(2)——基本概念与数据类型
JavaScript:学习笔记(2)——基本概念与数据类型 语法 1.区分大小写.Test 和 test 是完全不同的两个变量. 2.语句最好以分号结束,也就是说不以分号结束也可以. 变量 1.JS的 ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- Java学习笔记之---基础语法
Java学习笔记之---基础语法 一. Java中的命名规范 (一)包名 由多个单词组成时,所有字母小写(例如:onetwo) (二)类名和接口 由多个单词组成时,所有单词首字母大写(例如:OneTw ...
- opencv学习笔记(三)基本数据类型
opencv学习笔记(三)基本数据类型 类:DataType 将C++数据类型转换为对应的opencv数据类型 OpenCV原始数据类型的特征模版.OpenCV的原始数据类型包括unsigned ch ...
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样
python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入 ...
随机推荐
- 编译NDK的source code一定要用release mode!
编译NDK的source code一定要用release mode! 编译NDK的source code一定要用release mode! 编译NDK的source code一定要用release m ...
- 黄聪:bootstrap的模态框modal插件在苹果iOS Safari下光标偏离问题解决方案
一行CSS代码搞定: body.modal-open { position: fixed; width: 100%; }
- 黄聪:移动应用抓包调试利器Charles
一.Charles是什么? Charles是在 Mac或Windows下常用的http协议网络包截取工具,是一款屌的不行的抓包工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了 ...
- url参数的转码和解码
encodeURI(str) //转码 decodeURI(str)//解码
- Java连接S3并上传Redis
package com.shinho.bigdatalake.redis; import com.amazonaws.regions.Region; import com.amazonaws.regi ...
- WINDOWS和linux下stdcall
__stdcall被这个关键字修饰的函数,其参数都是从右向左通过堆栈传递的(__fastcall 的前面部分由ecx,edx传), 函数调用在返回前要由被调用者清理堆栈.这个关键字主要见于Micros ...
- ES6基础二(数组)
JSON数组格式转换 JSON的数组格式就是为了前端快速的把JSON转换成数组的一种格式:在普通的JSON最后多了一个length属性,就可以使用ES6的语法转变成数组. 当然了,不是所有的j ...
- react事件中的事件对象和常见事件
不管是在原生的js还是vue中,所有的事件都有其事件对象,该事件对象event中包含着所有与事件相关的信息,在react中,所有的事件也有其事件对象,在触发DOM上的某个事件时,就会产生一个事件对象. ...
- 继续循环continue
继续循环continue continue的作用是仅仅跳过本次循环,而整个循环体继续执行. 语句结构: for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) { continue; } ...
- Oracle空查询删除