博客

Ruby 循环控制中break,next,redo的用法区别

by zy at almost 8 years ago, last updated at almost 8 years ago
R

break

break停止正在执行的动作,直接跳出循环。

puts  "break"
a = [ 1 ,2 ,3 ,4 ,5 ,6 ]
a.each{ |x|
  if  x == 4
    break
  else
      puts x
  end
}
break
1
2
3

next

next会跳过现在的这一次循环,直接开始执行下一次循环的动作。

puts  "next"
a = [ 1 ,2 ,3 ,4 ,5 ,6 ]
a.each{ |x|
  if  x == 4
    next
  else
      puts x
  end
}

next
1
2
3
5
6

redo

redo与next很相似,但redo会以相同的条件重新执行这一次循环。

puts  "redo"
i = 0 
[1,2,3,4,5].each{ |x|
  i += 1 
    if i ==4
      redo
    end
    p [i,x]
}
redo
[1, 1]
[2, 2]
[3, 3]
[5, 4]
[6, 5]

简介的ruby代码

by 卡布卡布 at almost 8 years ago, last updated at almost 8 years ago
S

1.列表中每项乘2

p (1..10).map {|n| n * 2}

2.列表中数字求和

p (1...1000).inject  { |sum,n| sum + n}

3.检查字符串是否包括某个单词

words = ["hi", "hello", "perfect", "sum", "sup"]

example = "hello world i'm the super man "

p words.any? {|word| example.include? (word)}     #=>  sure

4.过滤数列

p [33, 44, 55, 66, 77, 88].partition {|n| n > 60 }  # =>  [[66, 77, 88], [33, 44, 55]]

5.生日快乐

2.times  {|n| puts "Happy Birthday #{n = 2 ? "dear cabda " : "to you"}"}

Block02

by Mafeng at almost 8 years ago, last updated at almost 8 years ago
G

Blocks can be Object

class ProcExample
 def pass_in_block(&action)
  @stored_proc = action
 end

 def use_proc(parameter)
  @store_proc.call(parameter)
 end
end

eg = ProcExample.new
eg.pass_in_block { |param| puts "The parameter is #{param}" }
eg.use_proc(99)
# => The parameter is 99

Block other method

def n_times(thing)
 lambda {|n| thing * n}
end

p1 = n_times(23)
p1.call(3) #=> 69
p2.call(4) #=> 92

call 可以回调block的方法并传递参数fgvber

对 block的再理解

by 卡布卡布 at almost 8 years ago, last updated at almost 8 years ago
S

1.block的多行与一行书写 多行书写

[1,2,3].each do |n|
    puts "num : #{n}"   #>  num is 1     num is 2    num is 3
end

一行书写

[1,2,3].each {|n| puts "num : #{n}"}   #>  num is 1     num is 2    num is 3

2.yield语句 可以看做是一种占位符当方法执行yield时候,实际执行的是调用方法时跟在后面的代码块中的代码. 当调用my_num 方法时,执行到yield时,会执行block语句中的内容,当block语句中的内容执行完后,又回回到my_num中继续执行。

def my_num

    puts "num is 1"     #>  num is 1

    yield                      #>  num is 2

    puts "num is 3"      #>  num is 3
end

my_num  do 

    puts "num is 2"

end

Learn 09 module 中included关键字的用法

by zy at almost 8 years ago, last updated at almost 8 years ago
R

我们已经知道 module中的inciude 关键字可以把模块中的方法嵌入为类中的实例方法,当然方法的作用域可以理解为继承父类,extend关键字可以把模块中的方法嵌入为类中的类方法。

除此之外,module中 有时候也需要嵌套其他模块,这时候就需要用到关键字included 和self 关键字 和base 关键字。代码如下:

module A
def self.included base
    base.include B
    base.extend C
end

module B
def hi 
    puts "This is B module"
end
end

module C
def hello
    puts "This is C module"
end
end

end

class ABC
include A
end

a = ABC.new
a.hi
ABC.hello

输出结果:


This is B module
This is C module

instance_eval 与 class_eval 的区别

by 小赓赓。 at almost 8 years ago, last updated at almost 8 years ago
W

instance_eval

首先要明白instance_eval是所有类的实例方法,打开的是当前实例作用域。所以

class A
end

a = A.new
a.instance_eval do 
  def hello
    '343'
  end
end

puts a.hello
#=>343

其次因为class类本身也是Class类的一个实例,所以instance其实也可以用在类上,所以也可以定义类方法

class A
end

A.instance_eval do 
  def class_method
    '343'
  end
end

puts A.class_method
#=>343

class_eval

首先class_eval是只有类才能调用的,class_eval会重新打开当前类作用域.

class A
end


A.class_eval do 
  def instance_methods
    puts '343'
  end
  def self.hi
    puts 'hi'
    puts self
  end
end

a = A.new
puts a.instance_methods
puts A.hi
#=>343
#=>hi
#=>A

可以看出在一个类上调用了class_eval,就可以在其中定义该类的实例方法。

所以

  • instance_eval必须由实例调用,可以用来定义单例方法
  • class_eval必须由类调用,可以用来定义实例方法

类方法的定义

by Mafeng at about 8 years ago, last updated at about 8 years ago
G

总结了一下类方法的定义:

#1
  class Person
    def self.species
      "Homo Sapien"
    end
  end
  #2
  class Person
    def Person.species
      "Homo Sapien"
    end
  end
  #3
  class Person
    class << self
      def species
        "Homo Sapien"
      end
    end
  end
  #4 
  class << Person
    def species
      "Homo Sapien"
    end
  end
  #5
  Person.instance_eval do
    def species
      "Homo Sapien"
    end
  end
#6
  class Foo
  end
  metaclass = (class << Foo; self; end)
  metaclass.class_eval do
      def species
        "Homo Sapien"
      end
    end
  end

最后一个还不理解,就先记下来.

对Hash的回顾

by 卡布卡布 at about 8 years ago, last updated at about 8 years ago
S

1.创建Hash的方法。方法一和方法二都是创建一个空hash,方法三创建的有两个 key-value值

 (1)x = Hash.new
 (2)x = {}
 (3)x = {:a => 1, :b => 2} 

2.Hash 的键值可以是任何目标,可以是数字、字符串、数组。

3.遍历Hash

x = {:a => 1, :b => 2, :c => 3}

  x.each  do  |k,v|  puts "#{k}  #{v}"  end

关于模块中include/extend/prepend

by 小赓赓。 at about 8 years ago, last updated at about 8 years ago
W

include

主要用于将一个类插入到一个类或者其他模块。在类中引入模块,使模块中的方法成为类的实例方法

module Management
  def company_notifies
    'company_notifies from management'
  end
end

class User
  include Management
end

user = User.new
puts user.company_notifies

extend

通常在类定义中引入模块,使模块中的方法成为类的类方法时使用

included

当模块被include时会被执行,同时会传递当前作用域的self对象。在类定义中引入模块,既希望引入实例方法,也希望引入类方法这个时候需要使用 include,但是在模块中对类方法的定义有不同,定义出现在方法def self.included(c) ... end 中

module Management
  def self.included base
    puts "Management is being included into #{base}"

    base.include InstanceMethods
    base.extend ClassMethods
  end

  module InstanceMethods
    def company_notifies
      'company_notifies from management'
    end
  end

  module ClassMethods
    def progress
      'progress'
    end
  end

end

class User
  include Management
end

puts '-' * 30
user = User.new
puts user.company_notifies

puts '-' * 30
puts User.progress

prepend 与include区别

  • include把模块注入当前类的继承链后面
  • prepend把模块注入当前类的继承链前面
module Management
  def company_notifies
    'company_notifies from management'
  end
end

class User
  prepend Management
  # include Management

  def company_notifies
    'company_notifies from user'
  end
end

p User.ancestors

puts '-' * 30
user = User.new
puts user.company_notifies
=>[Management, User, Object, Kernel, BasicObject]
# include   =>[User, Management, Object, Kernel, BasicObject]

=>company_notifies from management
# include   =>company_notifies from user

总结近期常用sublime的快捷键

by 小赓赓。 at about 8 years ago, last updated at about 8 years ago
W

1.Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)

2.Ctrl+G 跳转到相应的行

3.Ctrl+L 选择整行(按住-继续选择下行)

4.Ctrl+P 查找当前项目中的文件和快速搜索;输入 @ 查找文件主标题/函数

5.整行的上下移动:ctrl+shift+↑或ctrl+shift+↓

6.增加和减少缩进:ctrl+[ 或 ]

7.删除整行ctrl+shift+k

8.注释选中项/行:ctrl+/

博客总数:59