做网站买域名多少钱,深圳网站设计报价,html5手机网站欣赏,自动跳转入口免费ruby 基础
1、each、map、collect的区别
each: 仅遍历数组#xff0c;并做相应操作#xff0c;数组本身不发生改变。
map:遍历数组#xff0c;并做相应操作后#xff0c;返回新数组(处理)#xff0c;原数组不变。
collect: 跟map作用一样。
collect! map!: 多了一个作…ruby 基础
1、each、map、collect的区别
each: 仅遍历数组并做相应操作数组本身不发生改变。
map:遍历数组并做相应操作后返回新数组(处理)原数组不变。
collect: 跟map作用一样。
collect! map!: 多了一个作用改变原数组。
// 终端打开 irb
// each
a [ a, b, c, d ]
b a.each { |x| x ! } // a b [ a, b, c, d ]// map map!
a [ a, b, c, d ]
b a.map { |x| x ! }
a // [a, b, c, d]
b // [a!, b!, c!, d!]
c a.map! { |x| x ! }
a // [a!, b!, c!, d!]
c // [a!, b!, c!, d!]// collect collect!
a [ a, b, c, d ]
b a.collect { |x| x ! }
a // [a, b, c, d]
b // [a!, b!, c!, d!]
c a.collect! { |x| x ! }
a // [a!, b!, c!, d!]
c // [a!, b!, c!, d!]2、Block, lambda, Proc 的区别 proc和lambda都是对象而block不是 block是proc的实例proc可以重复使用函数参数只能有一个block但可以有多个proc # 说明block是proc的一个实例
def what_am_i(block) block.class
end
puts what_am_i {} # Procdef multiple_procs(proc1, proc2)proc1.callproc2.call
end
a Proc.new { puts First proc }
b Proc.new { puts Second proc }
multiple_procs(a,b)lambda会检查参数个数proc不会。 lam lambda { |x| puts x } # creates a lambda that takes 1 argument
lam.call(2) # prints out 2
lam.call # ArgumentError: wrong number of arguments
lam.call(1,2,3)
pro Proc.new { |x| puts x } # creates a proc that takes 1 argument
pro.call(2) # 2
pro.call # returns nil
pro.call(1,2,3) # 1lambda和proc对于return处理方式不同 lambda一个函数调用lambdalambda里return后返回函数接着执行。 proc: 一个函数调用procproc里return后 函数也会return。 def lambda_testlam lambda { return }lam.callputs Hello world
end
lambda_test
def proc_testproc Proc.new { return }proc.callputs Hello world
end
proc_test参考资料
What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?
浅谈Ruby中的block, proc, lambda, method object的区别
ruby中闭包
在Ruby里如果该方法返回一个代码块而该代码块获得了局部变量的引用则这些局部变量将在方法返回后依然可以访问。
def show(name)count 1;return proc do |value|puts count 1;puts name 我是闭包 valueend
endsh show(ruby)
sh.call(213)
sh.call(kkk)参考
Ruby的4种闭包blocks, Procs, lambdas 和 Methods
3、alias 的用法, alias 与 alias_method 的区别
alias: 给已经存在的方法或变量设置一个别名在重新定义已存在的方法时还可以通过别名来调用原来的方法。
alias_method: 和alias类似但只能给方法起别名是Module的一个私有实例方法。
class Demo def helloputs old_hell0end# 给hello方法取别名# alias old_hello helloalias_method :old_hello, :hellodef helloputs new_helloend
end
obj Demo.new
obj.hello
obj.old_hello
参考
https://www.cnblogs.com/smallbottle/p/3968704.html
https://ruby-china.org/topics/27747
4、Ruby的对象体系 ------------| BasicObject|------------^|superclass|--------- superclass-----------| Object | ^----------| Module |--------------- ----------^ | ^|superclass | |Superclass| | |
--------- -------- --- --------
| hello | class | String | class | Class |
| ---------- --------- ------
--------- --------- -------- |^ || class |-----------
hello.class # String
String.class # Class
String.superclass # Object
Class.class # Class
Class.superclass # Module
Module.class # Class
Module.superclass # Object
Object.class # Object
Object.superclass # BasicObject
BasicObject.class # Class
BasicObject.superclass # nilModule是Class的父类Class和Module的差别是Class是一个增强的Module它比Module多了一个newallocate方法除了这一个区别当然这个区别特别重要Class和Module基本是一样的。
参考 https://blog.csdn.net/feigeswjtu/article/details/51040006
5、 Include, Extend, Load, Require 的使用区别
include和extend用于在类中引入模块区别 include把模块中的方法作为类的实例方法 extend: 把模块中的方法作为类的类方法 module Adef class_typeputs This class is of type:#{self.class}end
end
class B extend A end
class C include A end
B.class_type
C.new.class_typerequire、load用于加载库如.rb文件区别 require: 加载一个库并且只加载一次如果多次加载会返回false。文件不需要扩展名 load: 加载一个库可多次加载。需要指定文件扩展名。 # 在当前目录中有一个 two.rb文件
require ./two;
load ./two.rb;6、yield、yield self
所有的方法(methods)都会隐式的带一个块(block)参数。 yield用来执行块参数也可以通过call()方法执行。 yield 相当于是 block.call() 方法的调用所以参数个数也需要对应, method 定义中 block 参数必须在最后 def foo1()yield 1,2,3 # 这里的 1 2 3 就是传递的参数
end
def foo2(time, block)yield 1, 2, 3block.call(5, 6, 7) puts time
end
foo1 {|x,y,z| puts z} # 3
foo2(2018) {|x,y,z| puts z} # 3 7 2018yield self 在一个对象中self 表示是一个当前对象的引用。 yield self if block_given? 相当于如果有块参数那个调用快参数参数是自己。 def foo(block)puts selfyield self if block_given?yield AAAAAAAAA
end
foo {|x| puts x}
foo参考链接https://www.cnblogs.com/licongyu/p/5522027.html
7、 Ruby中的元编程
Ruby中的元编程是可以在运行时动态地操作语言结构如类、模块、实例变量等的技术。你甚至于可以在不用重启的情况下在运行时直接键入一段新的Ruby代码并执行它。
Ruby的元编程也具有“利用代码来编写代码”的作用。例如常见的attr_accessor等方法就是如此。
7.1 实例变量、方法、类
同一个类的实例可以有不同的实例变量因为实例变量只有使用的时候才会被建立。匿名类对一个具体对象添加方法是ruby会插入一个新的匿名类来容纳新建的方法匿名类不可见。def initialize(*args): 方法可以通过参数不同执行不同的操作
7.2 send、eval、respond_to? send send是Object类的实例方法。第一个参数是方法名后面是方法的参数 使用send方法你所想要调用的方法就顺理成章的变成了一个普通的参数。你可以在运行时直至最后一刻自由决定到底调用哪个方法。 class Rubyistdef welcome(*args)Welcome args.join( )end
end
obj Rubyist.new
puts(obj.send(:welcome, hello, world))
eval(obj.welcome(123))eval 用于执行一个用字符串表示的代码,eval方法可以计算多行代码使得将整个程序代码嵌入到字符串中并执行成为了可能。eval方法很慢在执行字符串前最好对其预先求值。 str Hello
puts eval(str world) # Hello worldrespond_to? 所有的对象都有此方法使用respond_to?方法你可以确定对象是否能使用指定的方法。 obj Object.new
if obj.respond_to?(:program)obj.program
elseputs Sorry, the object doesnt understand the program message.
end参考资料http://deathking.github.io/metaprogramming-in-ruby/chapter04.html
参考资料
Ruby中的元编程
8、全局变量实例变量局部变量类变量Symbol
格式名称作用范围举例$开头全局变量从定义到程序结束$name开头实例变量selfname开头类变量内部直接用外部用类名::变量名name[a-z_]开头局部变量定义的类、模块、方法内部在类、模块、方法间不能共享name[A-Z]常量内部、外部均可外部访问类名::常量名NAME:开头Symbol内外:name
参考资料https://blog.csdn.net/emerald0106/article/details/7358766
9、ruby的特点
解释型执行方便快捷:Ruby是解释型语言其程序无需编译即可执行语法简单、优雅:完全面向对象: Ruby从一开始就被设计成纯粹的面向对象语言因此所有东西都是对象例如整数等基本数据类型。内置正则式引擎适合文本处理:Ruby支持功能强大的字符串操作和正则表达式检索功能可以方便的对字符串进行处理。自动垃圾收集: 具有垃圾回收Garbage CollectGC功能能自动回收不再使用的对象。不需要用户对内存进行管理。跨平台和高度可移植性:Ruby支持多种平台在Windows Unix Linux MacOS上都可以运行。Ruby程序的可移植性非常好绝大多数程序可以不加修改的在各种平台上加以运行。有优雅、完善的异常处理机制: Ruby提供了一整套异常处理机制可以方便优雅地处理代码处理出错的情况。
缺点
解释型语言所以速度较慢静态检查比较少
rails
active record
创建、查询、修改、删除
class Product ApplicationRecord
end
# 创建
# create 方法会创建一个新记录并将其存入数据库
user User.create(name: David, occupation: Code Artist)
# new 方法实例化一个新对象但不保存
user User.new
user.name David# 读取
users User.all # 返回所有用户组成的集合
user User.first # 返回第一个用户
david User.find_by(name: David) # 返回第一个名为 David 的用户
# 查找所有名为 David职业为 Code Artists 的用户而且按照 created_at 反向排列
users User.where(name: David, occupation: Code Artist).order(created_at: :desc)# 更新
user.name Dave
user.save #
user.update(name: Dave)# 删除
user.destroy
# https://ruby-china.github.io/rails-guides/active_record_basics.html#create
上面的代码会创建 Product 模型对应于数据库中的 products 表。同时products 表中的字段也映射到 Product 模型实例的属性上。
关联模型
关联原因 操作两个有关系的表时简化操作。
如果两个表有关联比如一个作者表一个图书表一个作者对应多本书如果此时删除一个作者前要先把作者对应的书删除然后在删除作者。但是关联后只需要在模型中删除作者语句即可删除书的动作active_record会自动完成。 belongs_to:一对一关系 belongs_to 关联创建两个模型之间一对一的关系声明所在的模型实例属于另一个模型的实例。 写belongs_to的模型里的一条记录相对于另一个表里有唯一一条记录叫一对一。 例如有作者和图书两个模型而且每本书只能指定给一位作者在图书表中写上 class Book ApplicationRecordbelongs_to :author
endhas_one一对一关系 has_one 关联也建立两个模型之间的一对一关系但语义和结果有点不一样。这种关联表示模型的实例包含或拥有另一个模型的实例。例如应用中每个供应商只有一个账户可以这么定义供应商模型 class Supplier ApplicationRecordhas_one :account
endhas_one与belongs_to的选择 has_many 一对多 has_many 关联表示模型的实例有零个或多个另一模型的实例。例如对应用中的作者和图书模型来说作者模型可以这样声明 class Author ApplicationRecordhas_many :books
end:through: 通过第三张表来关联 has_many :through 这种关联表示一个模型的实例可以借由第三个模型拥有零个和多个另一模型的实例。例如在医疗锻炼中病人要和医生约定练习时间。这中间的关联声明如下 class Physician ApplicationRecordhas_many :appointmentshas_many :patients, through: :appointments
end
class Appointment ApplicationRecordbelongs_to :physicianbelongs_to :patient
end
class Patient ApplicationRecordhas_many :appointmentshas_many :physicians, through: :appointments
endhas_one :through 这种关联表示一个模型通过第三个模型拥有另一模型的实例。例如每个供应商只有一个账户而且每个账户都有一个账户历史那么可以这么定义模型
class Supplier ApplicationRecordhas_one :accounthas_one :account_history, through: :account
endclass Account ApplicationRecordbelongs_to :supplierhas_one :account_history
endclass AccountHistory ApplicationRecordbelongs_to :account
endhas_and_belongs_to_many 直接建立两个模型之间的多对多关系不借由第三个模型。例如应用中有装配体和零件两个模型每个装配体有多个零件每个零件又可用于多个装配体这时可以按照下面的方式定义模型 class Assembly ApplicationRecordhas_and_belongs_to_many :parts
endclass Part ApplicationRecordhas_and_belongs_to_many :assemblies
end参考资料 https://ruby-china.github.io/rails-guides/association_basics.html
查询接口 检索对象 find # 检索指定主键对应的对象没有记录find方法抛出 ActiveRecord::RecordNotFound 异常。
client Client.find(10) # 查找主键ID为 10 的客户take # 检索一条记录而不考虑排序没有记录take方法返回 nil而不抛出异常。
client Client.take # SELECT * FROM clients LIMIT 1first # 默认查找按主键排序的第一条记录。如果没有记录first 方法返回 nil而不抛出异常
client Client.first # SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1last # 默认查找按主键排序的最后一条记录。没有记录last方法返回nil而不抛出异常。
client Client.last # SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
client Client.last(3) # 返回不超过指定数量的查询结果。
client Client.order(:first_name).last # 返回按照指定属性排序的最后一条记录。
client Client.last! # 和 last 方法类似,区别是没有记录会抛出 ActiveRecord::RecordNotFound 异常。find_by # 查找匹配指定条件的第一条记录。
Client.find_by first_name: Lifo # Client.where(first_name: Lifo).takefind_each # 批量检索记录每条记录传入块
User.find_each do |user|NewsMailer.weekly(user).deliver_now
end 条件查询 where 方法用于指明限制返回记录所使用的条件相当于 SQL 语句的 WHERE 部分。条件可以使用字符串、数组或散列指定。 纯字符串条件:容易受到 SQL 注入攻击的风险。
Client.where(orders_count 2) # 查找所有 orders_count 字段的值为 2 的客户记录。数组条件: 后面的参数会替换前面的问号()
Client.where(orders_count ?, params[:orders])
Client.where(orders_count ? AND locked ?, params[:orders], false)条件中的占位符
Client.where(created_at :start_date AND created_at :end_date,{start_date: params[:start_date], end_date: params[:end_date]})散列条件
Client.where(locked: true) # SELECT * FROM clients WHERE (clients.locked 1)NOT 条件先调用没有参数的 where 方法然后马上链式调用 not 方法就可以生成这个查询。
Client.where.not(locked: true) # SELECT * FROM clients WHERE (clients.locked ! 1) 排序 按 created_at 字段的升序方式取回记录
Client.order(:created_at)还可以使用 ASC升序 或 DESC降序 指定排序方式
Client.order(created_at: :desc)
Client.order(created_at: :asc)
Client.order(orders_count: :asc, created_at: :desc)
数据库基本操作 插入数据 insert into tablename (field1,field2,field3..) values (value1,value2,value3);查询 select * from tablename;
select */filed select from tablename where fieldkey;
select */field form tablename where field like %value%; // 模糊查询删除Delete delete from tablename where 条件和查询时条件类似更新update) update tablename set fieldvalue where 条件(和查询时类似;参考链接https://blog.csdn.net/yuanmxiang/article/details/51683232
https://www.cnblogs.com/daxueshan/p/6687521.html
其他面试题
https://www.jianshu.com/p/cc9f521d72ee