Ruby.new

1.?Ruby is a genuine object-orientedlanguage. Everything you manipulate is an object, and the results of thosemanipulations are themselves objects.

2.?Every object has a unique objectidentifier (abbreviated as object ID).

3.??puts is a standardRuby method that writes its argument(s) to the console, adding a newline aftereach.

4.? You don’t need semicolons at the endsof statements as long as you put each statement on a separate line.

5.??Ruby comments start with a # characterand run to the end of the line.

6.??Methods are defined with the key word def,followed by the method name and the method’s parameters between parentheses. (Infact, the parentheses are optional) Ruby doesn’t use braces to delimit thebodies of compound statements and definitions. Instead, you simply finish thebody with the keyword end.

7.??Ruby has many ways to create a string object,but probably the most common is to use string literals, which are sequences ofcharacters between single or double quotation marks. The difference between thetwo forms is the amount of processing Ruby does on the string whileconstructing the literal. In the single-quoted case, Ruby does very little. Inthe double-quoted case, Ruby does more work. First, it looks for substitutions(sequences that start with a backslash character) and replaces them with somebinary value. The most common of these is \n, which is replaced with a newlinecharacter. The second thing that Ruby does with double-quoted strings isexpression interpolation. Within the string, the sequence #{expression} is replacedby the value of expression.

8.??capitalize method, definedfor all strings, is to return the string with a leading uppercase letter.

9.?The value returned by a Ruby method isthe value of the last expression evaluated, so we can get rid of the temporaryvariable and the return statement altogether.

10.?The first characters of a nameindicate how the name is used. Local variables, method parameters, and methodnames should all start with a lowercase letter or an underscore. Globalvariables are prefixed with a dollar sign ($), and instance variables begin withan “at” sign (@). Class variables start with two “at” signs (@@). Finally,class names, module names, and constants must start with an uppercase letter.

11.?Both of arrays and hashes storecollections of objects, accessible using a key. With arrays, the key is aninteger, whereas hashes support any object as a key. Both arrays and hashesgrow as needed to hold new elements. It’s more efficient to access array elements,but hashes provide more flexibility. Any particular array or hash can holdobjects of differing types.

12.?You can create and initialize a newarray object using an array literal—a set of elements between square brackets.Ruby array indices start at zero.

13.?nil is an object,just like any other, that happens to represent nothing.

14.?Sometimes creating arrays of words canbe a pain, what with all the quotes and commas. Fortunately, Ruby has ashortcut; %w does just what we want: a = %w{ ant bee cat dog elk }

15.?A hash literal uses braces rather thansquare brackets. The literal must supply two objects for every entry: one forthe key, the other for the value. The key and value are normally separated by =>.Thething to the left of the => is the key, and the thing tothe right is the corresponding value. Hashes are indexed using the same squarebracket notation as arrays. a hash by default returns nil when indexed by a keyit doesn’t contain.

16.?p method that writesthe values to the console works like puts but displays values such as nilexplicitly.

17.?Symbols are simply constant names thatyou don’t have to pre-declare and that are guaranteed to be unique. A symbolliteral starts with a colon and is normally followed by some kind of name. There’sno need to assign some kind of value to a symbol. Ruby guarantees that nomatter where it appears in your program, a particular symbol will have the samevalue.

18.?You can use name: value pairs tocreate a hash if the keys are symbols.

19.?Ruby uses the keyword endto signify the end of a body of all the control structures.

20.?Most statements in Ruby return avalue, which means you can use them as conditions. And Ruby treats nilas a falsevalue in conditions.

21.?gets returns the nextline from the standard input stream or nil when the end of the file isreached.

22.?Ruby statement modifiers are a usefulshortcut if the body of an if or while statement is just asingle expression. Simply write the expression, followed by ifor whileand the condition: puts “Danger, Will Robinson” if radiation > 3000

23.?In Ruby, you typically create aregular expression by writing a pattern between slash characters (/pattern/).

24.?The pipe character “|”means “either the thing on the right or the thing on the left”. \s,matches a whitespace character (space, tab, newline, and so on); \d,which matches any digit; and \w, matches any character that mayappear in a typical word. A dot “.” matches (almost) any character.

25.?The match operator =~can be used to match a string against a regular expression. If the pattern isfound in the string, =~ returns its starting position;otherwise, it returns nil.

i.e.

ine = gets

if line =~ /Perl|Python/

puts “Scripting language mentioned: #{line}”

end

26. The part of a string matched by a regularexpression can be replaced with different text using one of Ruby’s substitutionmethods: sub which is to replace the first matched text and gsubwhich is to replace all the matched text. line.sub(/Perl/, ‘Ruby’)

27.?Code blocks are chunks of code you canassociate with method invocations, almost as if they were parameters. You canuse code blocks to implement callbacks (but they’re simpler than Java’sanonymous inner classes), to pass around chunks of code (but they’re moreflexible than C’s function pointers), and to implement iterators.

28.?Code blocks are just chunks of codebetween braces or between do and end. The braces bind more tightly thanthe do/endpairs. What is becoming a Ruby standard is to use braces for single-line blocksand do/endfor multiline blocks.

29.?All you can do with a block isassociate it with a call to a method. You do this by putting the start of theblock at the end of the source line containing the method call. If the methodhas parameters, they appear before the block. A method can then invoke anassociated block one or more times using the Ruby yield statement. You canthink of yield as being something like a method call that invokes theblock associated with the call to the method containing the yield.

30.?You can provide arguments to the callto yield,and they will be passed to the block. Within the block, you list the names ofthe parameters to receive these arguments between vertical bars (|params…|).

31.?Code blocks are used throughout theRuby library to implement iterators, which are methods that return successiveelements from some kind of collection.

32.?[ ‘cat’, ‘dog’, ‘horse’ ].each {|name|print name, ” ” }

5.times { print “*” }

3.upto(6) {|i| print i }

(‘a’..’e’).each {|char| print char }

Here we ask an array to call the block once for each ofits elements. Then, object 5 calls a block five times. Ratherthan use for loops, in Ruby we can ask the number 3 to call a block,passing in successive values until it reaches 6. Finally, the range ofcharacters from a to e invokes a block using the method each.

33.?printf prints itsarguments under the control of a format string (just like printf in C or Perl).

34. The array ARGV contains each of thearguments passed to the running program. The variable ARGF is a special kind ofI/O object that acts like all the contents of all the files whose names arepassed on the command line (or standard input if you don’t pass any filenames).

灯红酒绿的城市,登上楼顶,俯视万家灯火,

Ruby.new

相关文章:

你感兴趣的文章:

标签云: