`
yzhong_sa
  • 浏览: 88740 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类

my way in ruby第3课

阅读更多
容器:

数组:

数组的取值在ruby真他妈的松散  简直就乱来  不过倒是挺方便的。

分段取值:a[index,count] 从index位开始 取count位。
还有第二种写法
a[index..count] 效果一样
a[index...count] 不同 是不取最后一位。。


真他妈的乱来。。


哈希表:ruby的哈希表没有泛型的限制 要装什么就放什么  又是没有规则的鬼东西。。没限制是最大的限制。


好  今天是ruby 之旅的第三天 继续革命

Block:

感觉ruby的block是用来迭代的时候非常方便,尤其是在遍历查找的时候更是tmd流畅。

demo:
class Woman
attr_reader:name
attr_reader:age
def initialize(name,age)
@name = name
@age = age
end
def getName
  return @name
end
end

require 'Woman'
class Women
def initialize
  @womanList = Array.new
end
def [](key)
  if key.kind_of?(Integer)
   result =  @womanList[key]
  else
    @womanList.find {|woman|
     if woman.name == key
     result = woman
     end
    }
  end
  return result
end

def append(woman)
  @womanList.push(woman)
  self
end
end
allWomen = Women.new
allWomen.append(Woman.new('brandy',22))
allWomen.append(Woman.new('zhiling',24))
puts allWomen['brandy'].name


好 下面看看怎么实现迭代器。

Iterator:
郁闷 看到yield的时候就晕了。。。

什么鬼东西,感觉是自调用的东西

class YieldTest
#defien a block
def add(max)
  i1 = 1
  i2 = 1

  while i1 < max
   yield i1
  
   i1=i2
   i2 = i1+i2
  end
end

end

test = YieldTest.new
test.add(100) {|total| puts total}

这里感觉yield是在调用block的内容  并且带i1为参数去实行block了 block指{中的操作}。嗯
和论坛上那群鸟人说的continue的作用差不多  只是带个参数而已。

暂时理解成yield出现时就调用{}里面的操作就行了  不要想太多  深入的东西看多了打击士气啊。。
以后慢慢理解 再跟那群鸟人叼过。。

看ruby里面的实现机理真是收获不浅  老衲受惊了。。

ruby主要有三个迭代器 ,each find 还有 collect

试用一个each来遍历文件的读取吧:

def openFile
  f = File.open("testFile.txt")
  f.each {|line|
   puts line
  }
end
那个testFile自己叼出来试一下就行了。 新建类的操作不用我说了吧。。

分享到:
评论

相关推荐

    the-ruby-way

    the ruby way the ruby way

    The Ruby Way(第2版)

    The Ruby Way(第2版) &lt;br&gt;The Ruby Way assumes that the reader is already familiar with the subject matter. Using many code samples it focuses on "how-to use Ruby" for specific applications, either ...

    The Ruby Way--3rd Edition--2015-英文版

    The Ruby Way 第三版(英文版),全书22章,书中包含600多个按主题分类的示例。每个示例都回答了“如何使用Ruby来完成”的问题。 ——Ruby on Rails之父David Heinemeier Hansson倾力推荐!

    ruby学习资源(Programming Ruby, Learning Ruby, The Ruby Way)

    内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm

    THE RUBY WAY(中文版)(第二版)pdf

    《The Ruby Way 第二版》中文版采用“如何解决问题”的方式阐述Ruby编程,书中包含400多个按主题分类的示例。每个示例都回答了“如何使用Ruby来完成”的问题。首先对要完成的任务进行了描述,并讨论了技术方面的约束...

    Programming Ruby

    Are you frustrated with demanding languages that seem to get in your way, instead of getting the work done? If so, then we've got a language and book for you! Ruby is a fully object-oriented ...

    the ruby way 2ed

    the ruby way 2ed. the ruby way 2ed.

    The ruby way

    the ruby way,ruby 入门经典

    Ruby Under a Microscope

    express algorithms in a very natural way, and then it’s just a matter of typing ruby at the command line and pressing enter, and your Ruby script is running. However, Ruby’s syntax is deceptively ...

    the ruby way (2nd Edition)

    TheRubyWay(Ruby之道)英文版 Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.pdf

    Build Awesome Command-Line Applications in Ruby 2

    With its simple commands, flags, and parameters, a well-formed command-line application is the quickest way to automate a backup, a build, or a deployment and simplify your life. With this book, you...

    ruby and watir 安装指南

    C:\&gt;gem envRubyGems Environment: - RUBYGEMS VERSION: 1.3.7 - RUBY VERSION: 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] - INSTALLATION DIRECTORY: c:/ruby/lib/ruby/gems/1.8 - RUBY EXECUTABLE: c:/...

    11.5 时间日期the ruby way.rar

    11.5 时间日期the ruby way.rar

    koa-router-find-my-way:koa的路由器中间件。 基于find-my-way,一个基于http基的疯狂快速路由器

    考阿路由器找到我的路 用于路由器... 旧版本: koa-router-find-my-way @ 3 =&gt; find-my-way @ 2 koa-router-find-my-way @ 2 =&gt; find-my-way @ 1基本用法const koaRouter = require ( 'koa-router-find-my-way' ) ;cons

    Ruby.Performance.Optimization

    You'll make sure slow code doesn't creep back into your Ruby application by writing performance tests, and you'll learn the right way to benchmark Ruby. And finally, you'll dive into the Ruby ...

    Ruby-Way.rar_About Language

    Ruby Way by Hal fulton its about Ruby language programming

    Ruby Performance Optimization, Why Ruby is Slow, and How to Fix It

    You’ll make sure slow code doesn’t creep back into your Ruby application by writing performance tests, and you’ll learn the right way to benchmark Ruby. And finally, you’ll dive into the Ruby ...

    Head First Ruby.pdf

    This Head First guide teaches you the Ruby language in a concrete way that gets your neurons zapping and helps you become a Ruby genius. You'll enter at Ruby's language basics and work through ...

    由MyWay壁纸「Wallpapers by MyWay」-crx插件

    MyWay的Wallpapers是您每天都在浏览主页和新标签的理想方式。 无论您的心情如何,都有免费的壁纸可以匹配它。 最重要的是,它是免费的。 无需注册。 立即加入我们的忠实用户,享受美丽,免费的动态壁纸。 现在点击...

Global site tag (gtag.js) - Google Analytics