【Ruby on Rails 学习三】Ruby 基本数据类型(类、类的实例、对象)
数字、文本、范围、符合、True、False、Nil
1为什么是一个类的对象,使用methods方法可以查看一个对象的所有函数(方法)
$ irb
irb(main)::>
=>
irb(main)::> .methods
=> [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :bit_length, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :remainder, :real?, :nonzero?, :step, :quo, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main)::>
在Ruby中万物皆对象,没有基本的数据类型
通过to_s这个方法 将 1 变成了字符串,这就是Ruby奇妙的地方
irb(main)::> .to_s
=> ""
irb(main)::>
为什么 0.4-0.3 不等于 0.1
irb(main)::> -
=>
irb(main)::> -3.5
=> 0.5
irb(main)::> 0.4-0.3
=> 0.10000000000000003
irb(main)::>
插值操作
irb(main)::> a =
=>
irb(main)::> b =
=>
irb(main)::> "hello #(a+b)"
=> "hello #(a+b)"
irb(main)::> "hello #{a+b}"
=> "hello 5"
范围
大于等于1 小于等于2 1<= <=2
大于等于1 小于3 1<= <3
irb(main)::> ..
=> ..
irb(main)::> ...
=> ...
irb(main)::> "hello"*
=> "hellohellohello"
irb(main)::>
irb(main)::> "hello".methods
=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :byteslice, :scrub, :scrub!, :freeze, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :<<, :prepend, :crypt, :intern, :to_sym, :ord, :include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr_s, :delete, :squeeze, :count, :tr!, :tr_s!, :delete!, :squeeze!, :each_line, :each_byte, :each_char, :each_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force_encoding, :b, :valid_encoding?, :ascii_only?, :unpack, :encode, :encode!, :to_r, :to_c, :>, :>=, :<, :<=, :between?, :nil?, :!~, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main)::>
irb(main)::> a = "hello"
=> "hello"
irb(main)::> a[]=""
=> ""
irb(main)::> a
=> "1ello"
irb(main)::>
bool类型
irb(main)::> ==
=> true
irb(main)::> ==
=> false
irb(main)::> true.methods
=> [:to_s, :inspect, :&, :|, :^, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main)::>
Nil
irb(main)::> "hello".nil?
=> false
irb(main)::> "".nil?
=> false
irb(main)::>
empty
irb(main)::> "".methods
=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :byteslice, :scrub, :scrub!, :freeze, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :<<, :prepend, :crypt, :intern, :to_sym, :ord, :include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr_s, :delete, :squeeze, :count, :tr!, :tr_s!, :delete!, :squeeze!, :each_line, :each_byte, :each_char, :each_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force_encoding, :b, :valid_encoding?, :ascii_only?, :unpack, :encode, :encode!, :to_r, :to_c, :>, :>=, :<, :<=, :between?, :nil?, :!~, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main)::> "".empty?
=> true
irb(main)::>
chomp
irb(main)::> a = gets => "100\n"
irb(main)::> puts a => nil
irb(main)::> a.chomp
=> ""
irb(main)::> a.chomp.to_i
=>
irb(main)::>
while循环的控制语句break和next
1 input = gets.chomp.to_i
2 while input!= 0
3 puts "break if input ==2,try try"
4 break if input == 2
5 next if input == 3
6 puts "跳过这个循环控制语句,跳出或者略过"
7 end
【Ruby on Rails 学习三】Ruby 基本数据类型(类、类的实例、对象)的更多相关文章
- 【Ruby on Rails学习二】在线学习资料的整理
由于工作任务重,时间紧,没有太多学习的时间,大致找了些在线学习资料,这里做个整理,希望对同样准备学习的朋友有帮助 在线文档类: Ruby on Rails 实战圣经 使用 Rails 4.2 及 R ...
- 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby
要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin 开发的一款命令行工具.rvm 能够让 ...
- Ruby on rails学习笔记——安装环境
出现问题: C:\Users\len>gem install rails ERROR: While executing gem ... (Gem::RemoteFetcher::FetchErr ...
- 【Ruby on Rails 学习四】简单的代码快和错误处理
第一个例子: 1 ... 5000的加法运算 1 sum = 0 2 i = 1 3 while true 4 sum += i 5 i += 1 6 break if i == 5001 7 end ...
- 【Ruby on Rails 学习五】Ruby语言的方法
1.方法的调用 2.自定义方法 3.带默认值的自定义方法 4.带返回值的自定义方法 方法或者说是函数,实际上是包含了一段代码,去执行某一个特定的过程. def add(a=3,b=2) return ...
- 【Ruby on Rails 学习六】Ruby 类 的入门
1.什么是类 2.类与实例的区别 3.自定义简单的类 生活中的垃圾分类,是集合上的概念 比如数学上的 1 a 2 b c 4 5分类为数字1 2 4 5 ,字母 a b c ir ...
- [ruby on rails] 深入(2) ruby基本语法
1. 调试&注释&打印输出 1.1 调试 ruby属于解释型语言,即脚本,在linux上,脚本的执行无非三种: 1. 用解释器运行脚本 解释器 脚本文件 即:ruby 脚本文件 2. ...
- Redis学习三:Redis数据类型
一.Redis的五大数据类型 1.String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安 ...
- Android JNI 学习(三):JNI 数据类型和数据结构
本文我们来讨论一下JNI如何将Java类型映射到本机C类型. 一.基本数据类型 如下图表整理了Java基本类型和native对应的关系: Java类型 Native类型 描述 boolean jboo ...
随机推荐
- TCP/IP 协议栈4层结构及3次握手4次挥手
TCP/IP 协议栈是一系列网络协议的总和,是构成网络通信的核心骨架,它定义了电子设备如何连入因特网,以及数据如何在它们之间进行传输.TCP/IP 协议采用4层结构,分别是应用层.传输层.网络层和链路 ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器
[文章作者:张宴 本文版本:v6.3 最后修改:2010.07.26 转载请注明原文链接:http://blog.zyan.cc/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx ...
- sizeof运算符、虚函数、虚继承考点(待修改)
参考: http://blog.csdn.net/wangyangkobe/article/details/5951248 下面的文章解释有错误,不要看.......... 记住几句话: 编译器为每个 ...
- 科普TPF知识
https://tieba.baidu.com/p/4926092734?see_lz=1&pn=1 707680700 https://tieba.baidu.com/p/492609273 ...
- BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)
题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...
- phpexcel 导出数字类型字段导出错误或者为空解决办法 (原)
跟我们写excel时候一样,手机号或者较长的数字类型,或被科学计数法和谐,但是如果类型是字符串,长一些的数字就不受影响了. 解决导出被和谐的最简单易懂的,就是最前面拼接‘ ’ 空格,或者字母符号之类, ...
- TensorFlow基本计算单元——变量
# -*- coding: utf-8 -*- import tensorflow as tf a = 3 # 创建变量 w = tf.Variable([[0.5, 1.0]]) #行向量 x = ...
- debug1: Could not open authorized keys
ssh登录的时候一直日志一直出现debug1: Could not open authorized keys登录不上,检查文件夹权限都正常用这条命令解决了 restorecon -FRvv /home ...
- Php mysql 常用代码、CURD操作以及简单查询
C/S:Client ServerB/S:Brower Server php主要实现B/S LAMP :Linux系统 A阿帕奇服务器 Mysql数据库 Php语言 mysql常用代码 ...
- OpenCV学习笔记(9)——形态学转换
不同的形态学操作,例如腐蚀,膨胀,开运算,闭运算等 形态学操作是根据图像形状进行的简单操作.一般情况下对二值化图像进行操作.需要的参数有两个,一个是原始图像,第二个被称为结构化元素或核,它是用来决定操 ...