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 下一个历史输入 ...
随机推荐
- TStrings (TStringList)很有功能
用 TStrings的Object 保存类的方式,来保存除了Items以外的值. 今天才发现,原来,TStrings下,还有 Items,Values,Items.Names,Items.Values ...
- RGB颜色空间、HSV颜色空间的理解
HSV是把H(色相),S(饱和度),V(亮度)当做色值来定位颜色的空间. 1.HSV模型 色相:取值范围是0~360度,用来表示颜色的类别.其中红色是0度,绿色是120度,蓝色是240度.饱和度:取值 ...
- Thinkphp 缓存和静态缓存局部缓存设置
1.S方法缓存设置 if(!$rows = S('indexBlog')){ //*$rows = S('indexBlog') $rows = D('blog')->select(); S(' ...
- SpringMVC-DispatcherServlet配置(Spring-servlet.xml)
Spring-servlet.xml <context:component-scan base-package="com.spring.mvc.controller"/> ...
- 在VMware中安装Mac OS
macOS与Darwin http://blog.csdn.net/hintcnuie/article/details/38468093 OS X 是整个操作系统的一个集体名称.而Darwin 就是其 ...
- 数据挖掘标准规范之CRISP-DM基础
一.前言 每每提到数据挖掘,总有些人上来就是ETL.是算法.是数学模型,作为搞工程实施的我而言,很是头疼.其实作为数据挖掘的而言,算法只是其实现手段.是工具和实现手段而已,我们不是在创造算法(国外职业 ...
- Ubuntu 14.10 下Spark on yarn安装
1 服务器分布 服务器 说明 192.168.1.100 NameNode 192.168.1.101 DataNode 192.168.1.102 DataNode 2 软件环境 2.1 安装JDK ...
- C#多线程更新,查询MongoDB数据
对多线程一直在用,但是从来没去深入研究过注意的事项,包括和锁的配合也是一知半解没有真正理解锁(lock)的用法. 这次在做数据分析是需要将访问的url中host部分记录,如果已经存在就直接匹配id,没 ...
- Visual Studio 2010 Shortcut
General Shortcut Description Ctrl-X or Shift-Delete Cuts the currently selected item to the clipboar ...
- PAT 乙级 1051 复数乘法 (15) C++版
1051. 复数乘法 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 复数可以写成(A + Bi)的常规 ...