- 浏览: 797151 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (651)
- Java (39)
- Java 初学者小问题 (66)
- 设计模式 (7)
- 项目管理 (3)
- 数据库 (1)
- 算法 (2)
- Java practices (6)
- Effective Java2读书笔记 (78)
- Linux (2)
- programming ruby 读书笔记 (5)
- Core Java Ninth Edition Volume I 读书笔记 (15)
- Pro Git 读书笔记 (12)
- Git (3)
- Maven in Action 读书笔记 (20)
- Web (12)
- 非技术类书籍 (11)
- 电影 (40)
- Web Cache (1)
- jquery (0)
- 历史 (4)
- Dive Into HTML5 读书笔记 (13)
- 三国演义小学毕业考 (79)
- 高效能人士的7个习惯 读书笔记 (12)
- Java Performance 读书笔记 (3)
- Protocol Buffer 学习笔记 (6)
- Mongo DB 学习笔记 (7)
- Morphia 学习笔记 (7)
- Algorithms -- Princeton 学习笔记 (13)
- String研究 (10)
- Hadoop: The Definitive Guide 读书笔记 (3)
- Java与模式读书笔记 (5)
- Date研究 (3)
- The Roman Empire 听课笔记 (4)
- Algorithms -- Standford 学习笔记 (16)
- Core Java Ninth Edition Volume II 读书笔记 (9)
- Thinking in Java 4th Edition 读书笔记 (21)
- Node : Up and Running 学习笔记 (5)
- Eloquent Javascript (8)
- Smashing Node.js 读书笔记 (1)
- Algorithms II -- Standford 学习笔记 (19)
- Algorithm II -- Princeton 学习笔记 (14)
- 网络安全 (2)
- Javascript (4)
- 正则表达式 (1)
- JAVA 7/8 (15)
- JVM (10)
- NodeJS (1)
- 鸟哥的linux私房菜读书笔记 (14)
- Web Service (1)
- The art of programming (9)
- Introduction to Algorithm 读书笔记 (4)
- Java 源码阅读 (0)
- Spring in Action 读书笔记 (2)
- Java Network Programming 读书笔记 (2)
最新评论
-
心存高远:
谢谢作者分享,刚好看到这里不太明白,现在茅塞顿开。不过runt ...
关于 Maven的传递依赖的理解 -
sxlkk:
851228082 写道甚至在某次技术会议现场遇到《Maven ...
关于 Maven的传递依赖的理解 -
851228082:
851228082 写道a----compile----b-- ...
第五章 坐标和依赖 -
851228082:
a----compile----b-----provided- ...
第五章 坐标和依赖 -
851228082:
甚至在某次技术会议现场遇到《Maven in action》的 ...
关于 Maven的传递依赖的理解
1. The class Array holds a collection of object references. Each object reference occupies a position in the array, identified by a non-negative integer index.
2. A literal array is simply a list of objects between square brackets(optional).
3. Arrays are indexed using the [ ] operator. As with most Ruby operators, this is actually a method (an instance method of class Array) and hence can be overridden in subclasses.
4. Index an array with a negative integer, and it counts from the end.
5. You can also index arrays with a pair of numbers, [start,count ]. This returns a new array consisting of references to count objects starting at position start.
6. You can index arrays using ranges, in which start and end positions are separated by two or three periods. The two-period form includes the end position; the three-period form does not.
7. The [ ] operator has a corresponding [ ]= operator, If used with a single integer index, the element at that position is replaced by whatever is on the right side of the assignment. Any gaps that result will be filled with nil.
a = [ 1, 3, 5, 7, 9 ] → [1, 3, 5, 7, 9] a[1] = ’bat’ → [1, "bat", 5, 7, 9] a[-3] = ’cat’ → [1, "bat", "cat", 7, 9] a[3] = [ 9, 8 ] → [1, "bat", "cat", [9, 8], 9] a[6] = 99 → [1, "bat", "cat", [9, 8], 9, nil, 99]
8. If the index to [ ]= is two numbers (a start and a length) or a range, then those elements in the original array are replaced by whatever is on the right side of the assignment. If the length is zero, the right side is inserted into the array before the start position; If the right side is itself an array, its elements are used in the replacement. The array size is automatically adjusted if the index selects a different number of elements than are available on the right side of the assignment:
a = [ 1, 3, 5, 7, 9 ] → [1, 3, 5, 7, 9] a[2, 2] = ’cat’ → [1, 3, "cat", 9] a[2, 0] = ’dog’ → [1, 3, "dog", "cat", 9] a[1, 1] = [ 9, 8, 7 ] → [1, 9, 8, 7, "dog", "cat", 9] a[0..3] = [] → ["dog", "cat", 9] a[5..6] = 99, 98 → ["dog", "cat", 9, nil, nil, 99, 98]
9. push and pop add and remove elements from the end of an array, so you can use the array as a stack.
10. unshift and shift add and remove elements from the head of an array. Combine shift and push , and you have a first-in first-out (FIFO) queue.
11. The first and last methods return the n entries at the head or end of an array without removing them.
12. Hashes (sometimes known as associative arrays, maps, or dictionaries) are similar to arrays in that they are indexed collections of object references. However, you can index a hash with objects of any type: symbols, strings, regular expressions, and so on.
13. If we want keys to be symbols, we can write the hash literal using either the old syntax with => or the new key: value syntax:
h = { dog: 'canine', cat: 'feline', donkey: 'asinine' } # same as... h = { :dog => 'canine', :cat => 'feline', :donkey => 'asinine' }
14. Ruby remembers the order in which you add items to a hash. When you subsequently iterate over the entries, Ruby will return them in that order.
15. downcase returns a lowercase version of a string, and scan returns an array of substrings that match a given pattern:
def words_from_string(string) string.downcase.scan(/[\w']+/) end
[\w’]+ , matches sequences containing “word characters” and single quotes.
16. When you use sort_by , you give it a block that tells the sort what to use when making comparisons. The result of the sort is an array containing a set of two-element arrays, with each subarray corresponding to a key/entry pair in the original hash:
counts.sort_by {|word, count| count}
17. The method each is an iterator—a method that invokes a block of code repeatedly.
18. You can think of a block as being somewhat like the body of an anonymous method. Just like a method, the block can take parameters (but, unlike a method, those parameters appear at the start of the block between vertical bars). Blocks can appear in Ruby source code only immediately after the invocation of some method.
19. If there’s a variable inside a block with the same name as a variable in the same scope outside the block, the two are the same. Parameters to a block are now always local to a block, even if they have the same name as locals in the surrounding scope:
value = "some shape" [ 1, 2 ].each {|value| puts value } puts value
20. You can define block-local variables by putting them after a semicolon in the block’s parameter list:
square = "some shape" sum = 0 [1, 2, 3, 4].each do |value; square| square = value * value # this is a different variable sum += square end
21. A Ruby iterator is simply a method that can invoke a block of code.
22. A common iterator is collect (also known as map ), which takes each element from the collection and passes it to the block. The results returned by the block are used to construct a new array:
["H", "A", "L"].collect {|x| x.succ } # => ["I", "B", "M"]
the succ method increments a string value.
23. The each_with_index method calls its block with two parameters: the current element of the iteration and the count (which starts at zero):
f = File.open("testfile") f.each_with_index do |line, index| puts "Line #{index} is: #{line}" end f.close
24. The inject method lets you accumulate a value across the members of a collection:
[1,3,5,7].inject(0) {|sum, element| sum+element} # => 16 [1,3,5,7].inject(1) {|product, element| product*element} # => 105
inject works like this: the first time the associated block is called, sum is set to inject ’s parameter, and element is set to the first element in the collection. The second and subsequent times the block is called, sum is set to the value returned by the block on the previous call. The final value of inject is the value returned by the block the last time it was called. If inject is called with no parameter, it uses the first element of the collection as the initial value and starts the iteration with the second value. You can also give it the name of the method you want to apply to successive elements of the collection:
[1,3,5,7].inject(:+) # => 16 [1,3,5,7].inject(:*) # => 105
addition and multiplication are simply methods on numbers, and :+ is the symbol corresponding to the method + .
25. You can create an Enumerator object by calling the to_enum method (or its synonym, enum_for ) on a collection such as an array or a hash. Most of the internal iterator methods will also return an Enumerator object if called without a block:
a = [ 1, 3, "cat" ] enum_a = a.each # create an Enumerator using an internal iterator enum_a.next # => 1 enum_a.next # => 3
26. Ruby has a method called loop that does nothing but repeatedly invoke its block. Typically, your code in the block will break out of the loop when some condition occurs. But loop is also smart when you use an Enumerator —when an enumerator object runs out of values inside a loop , the loop will terminate cleanly:
short_enum = [1, 2, 3].to_enum long_enum = ('a'..'z').to_enum loop do puts "#{short_enum.next} - #{long_enum.next}" end
27. The each_char method of strings will return an enumerator if you don’t give it a block, and you can then call each_with_index or with_index on that enumerator:
result = [] "cat".each_char.each_with_index {|item, index| result << [item, index] } result # => [["c", 0], ["a", 1], ["t", 2]]
28. You can also create the Enumerator object explicitly—create one that calls our string’s each_char method. We can call to_a on that enumerator to iterate over it:
enum = "cat".enum_for(:each_char) enum.to_a # => ["c", "a", "t"]
If the method we’re using as the basis of our enumerator takes parameters, we can pass them to enum_for :
enum_in_threes = (1..10).enum_for(:each_slice, 3) enum_in_threes.to_a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
29. You can create an explicit enumerator, passing it a block. The code in the block will be used when the enumerator object needs to supply a fresh value to your program. The block isn’t simply executed from top to bottom. Instead, the block is executed in parallel with the rest of your program’s code. Execution starts at the top and pauses when the block yields a value to your code. When the code needs the next value, execution resumes at the statement following the yield:
triangular_numbers = Enumerator.new do |yielder| number = 0 count = 1 loop do number += count count += 1 yielder.yield number end end
30. Enumerator objects are also enumerable (that is to say, the methods available to enumerable objects are also available to them).
31. we use the &block notation to pass the block as a parameter to the method :
triangular_numbers = Enumerator.new do |yielder| # ... as before end class Enumerator def infinite_select(&block) Enumerator.new do |yielder| self.each do |value| yielder.yield(value) if block.call(value) end end end end p triangular_numbers .infinite_select {|val| val % 10 == 0} .infinite_select {|val| val.to_s =~ /3/ } .first(5)
32. If File.open has an associated block, then that block will be invoked with a file object, and the file will be closed when the block terminates. This is interesting, because it means that File.open has two different behaviors.When called with a block, it executes the block and closes the file.When called without a block, it returns the file object. This is made possible by the method block_given? , which returns true if a block is associated with the current method.
33. If the last parameter in a method definition is prefixed with an ampersand (such as &action ), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter.
34. Both lambda and Proc.new take a block and return an object of class Proc .
35. Even though the parameter is out of scope by the time the block is called, the parameter remains accessible to the block. This is called a closure—variables in the surrounding scope that are referenced in a block remain accessible for the life of that block and the life of any Proc object created from that block:
def n_times(thing) lambda {|n| thing * n } end p1 = n_times(23) # thing is out of scope at this time, but it still works
36. Rather than write this:
lambda { |params| ... }
you can now write the following:
->params { ... } proc1 = -> arg { puts "In proc1 with #{arg}" } proc2 = -> arg1, arg2 { puts "In proc2 with #{arg1} and #{arg2}" } proc3 = ->(arg1, arg2) { puts "In proc3 with #{arg1} and #{arg2}" }
发表评论
-
Sharing Functionality: Inheritance, Modules, and Mixins
2012-09-01 12:39 10041. When puts needs t ... -
Classes, Objects, and Variables
2012-08-17 22:25 10611. initializ ... -
Ruby.new
2011-11-23 12:06 10291. Ruby is a genuine obje ... -
Getting Started
2011-11-18 10:16 9961. Interactive Ruby—is t ...
相关推荐
Docker Containers Build and Deploy with Kubernetes, Flannel, Cockpit, and Atomic 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Decide whether microservices and containers are worth your investment, and manage the organizational learning curve associated with them Apply best practices for interprocess communication among ...
■ How Docker can benefit you: I explain the benefits of creating and run- ning applications in containers, instead of installing software packages (in formats such as RPM or Deb) and running ...
Practical LXC and LXD Linux Containers for Virtualization and Orchestration 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn...
Cloud Native: Using Containers, Functions, and Data to Build Next-Generation Applications This comprehensive guide helps developers and architects understand current cloud-native technologies, with ...
Databases, containers, and pods- SQL Server on Kubernetes.pptx
Practical LXC and LXD begins by introducing you to Linux containers (LXC and LXD). You will then go through use cases based on LXC and LXD. Next, you will see the internal workings of LXC and LXD by ...
Docker Containers Build and Deploy with Kubernetes, Flannel, Cockpit, and Atomic 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络...
Chapter 0 Getting started 0.1 Comments 0.2 #include 0.3 The main function 0.4 Curly braces 0.5 Using the standard library for output 0.6 The return ...B.2 Containers and iterators B.3 Algorithms
In Microservices and Containers, longtime systems architect and engineering team leader Parminder Kocher analyzes two of the hottest new technology trends: microservices and containers. Together, as ...
With the updated edition of this practical guide, you’ll learn how to use Docker to package your applications with all of their dependencies and then test, ship, scale, and support your containers ...
Introduction to Containers, Kubernetes, and Red Hat OpenShift.zip
Microservices and containers, red-hot topics that, in my opinion, also possess staying power. Compared to the monolithic approach, the most common way of developing and deploying applications, ...
OpenStack gets tremendous traction as it is used by many organizations across the globe and as containers gain in popularity and become complex, it’s necessary for OpenStack to provide various ...
The C++ Standard Library: A Tutorial and Reference, Second... The book focuses in particular on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms.
标题中提到的“kata containers”和“gvisor”是指两种不同的容器运行环境。容器技术是一种轻量级的虚拟化技术,它通过隔离的方式共享单个操作系统的硬件资源,实现应用程序及其依赖的隔离运行。容器具有启动快速、...
### 控制反转容器与依赖注入模式 在Java社区中,一系列轻量级容器的涌现为不同项目组件的集成提供了一种高效的方式。这些容器背后的核心技术通常被统称为“控制反转”(Inversion of Control, IoC)。...