楼主: maybeone123
2071 17

Ruby Cookbook, 2nd Edition [推广有奖]

  • 2关注
  • 0粉丝

已卖:5份资源

大专生

8%

还不是VIP/贵宾

-

威望
0
论坛币
147 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
2974 点
帖子
9
精华
0
在线时间
78 小时
注册时间
2015-5-24
最后登录
2022-5-2

楼主
maybeone123 发表于 2015-10-31 13:54:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
ruby_cookbook_2nd_edition.jpg

Ruby Cookbook, 2nd EditionRecipes for Object-Oriented Scripting

Revised for Ruby 2.1, each recipe includes a discussion on why and how the solution works. You'll find recipes suitable for all skill levels, from Ruby newbies to experts who need an occasional reference. With Ruby Cookbook, you'll not only save time, but keep your brain percolating with new ideas as well.




Book Details


  • Publisher:        O'Reilly Media
  • By:        Lucas Carlson, Leonard Richardson
  • ISBN:        978-1-44937-371-9
  • Year:        2015
  • Pages:        922
  • Language:        English
  • File size:        9.5  MB
  • File format:        PDF
Download:      

本帖隐藏的内容

Ruby Cookbook, 2nd Edition.pdf (9.05 MB, 需要: 5 个论坛币)






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Edition editio dition Book reference includes solution experts recipes

已有 1 人评分经验 论坛币 收起 理由
Nicolle + 100 + 100 鼓励积极发帖讨论

总评分: 经验 + 100  论坛币 + 100   查看全部评分

本帖被以下文库推荐

沙发
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:46:59
  1. 2.1 Building a String from Parts
  2. Problem
  3. You want to iterate over a data structure, and build a string from the data at the same time.
  4. Solution
  5. There are two efficient solutions. The simplest solution is to start with an empty string, and repeatedly append substrings onto it with the << operator:
  6. hash = { key1: "val1", key2: "val2" }
  7. string = ""
  8. hash.each { |k,v| string << "#{k} is #{v}\n" }
  9. puts string
  10. # key1 is val1
  11. # key2 is val2
  12. This variant of the simple solution is slightly more efficient, but harder to read:
  13. string = ""
  14. hash.each { |k,v| string << k.to_s << " is " << v << "\n" }
  15. If your data structure is an array, or easily transformed into an array, it’s usually more efficient to use Array#join:
  16. puts hash.keys.join("\n") + "\n"
  17. # key1
  18. # key2
复制代码

藤椅
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:48:22
  1. 2.2 Substituting Variables into Strings
  2. Problem
  3. You want to create a string that contains a representation of a Ruby variable or expression.
  4. Solution
  5. Within the string, enclose the variable or expression in curly brackets and prefix it with a hash character:
  6. number = 5
  7. "The number is #{number}."                      # => "The number is 5."
  8. "The number is #{5}."                           # => "The number is 5."
  9. "The number after #{number} is #{number.next}."
  10. # => "The number after 5 is 6."
  11. "The number prior to #{number} is #{number-1}."
  12. # => "The number prior to 5 is 4."
  13. "We're ##{number}!"                             # => "We're #5!"
复制代码

板凳
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:51:01
  1. 2.3 Substituting Variables into an Existing String
  2. Problem
  3. You want to create a string that contains Ruby expressions or variable substitutions, without actually performing the substitutions. You plan to substitute values into the string later, possibly multiple times with different values each time.
  4. Solution
  5. There are two good solutions: printf-style strings, and ERB (meaning “embedded Ruby”) templates.
  6. Ruby supports a printf-style string format like C’s and Python’s. Put printf directives into a string and it becomes a template. You can interpolate values into it later using the modulus operator:
  7. template = 'Oceania has always been at war with %s.'
  8. template % 'Eurasia'  # => "Oceania has always been at war with Eurasia."
  9. template % 'Eastasia' # => "Oceania has always been at war with Eastasia."

  10. 'To 2 decimal places: %.2f' % Math::PI       # => "To 2 decimal places: 3.14"
  11. 'Zero-padded: %.5d' % Math::PI               # => "Zero-padded: 00003"
复制代码

报纸
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:51:50
  1. 2.4 Reversing a String by Words or Characters
  2. Problem
  3. The letters (or words) of your string are in the wrong order.
  4. Solution
  5. To create a new string that contains a reversed version of your original string, use the reverse method. To reverse a string in place, use the reverse! method:
  6. s = ".sdrawkcab si gnirts sihT"
  7. s.reverse                            # => "This string is backwards."
  8. s                                    # => ".sdrawkcab si gnirts sihT"

  9. s.reverse!                           # => "This string is backwards."
  10. s                                    # => "This string is backwards."
  11. To the order of the words in a string, split the string into a list of whitespaceseparated words, then join the list back into a string:
  12. s = "order. wrong the in are words These"
  13. s.split(/(\s+)/).reverse!.join('')   # => "These words are in the wrong order."
  14. s.split(/\b/).reverse!.join('')      # => "These words are in the wrong. order"
复制代码

地板
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:53:15
  1. 2.5 Representing Unprintable Characters
  2. Problem
  3. You need to make reference to a control character, a strange UTF-8 character, or some other character that’s not on your keyboard.
  4. Solution
  5. Ruby gives you a number of escaping mechanisms to refer to unprintable characters. By using one of these mechanisms within a double-quoted string, you can put any binary character into the string.
  6. You can reference any binary character by encoding its octal representation into the format "\000", or its hexadecimal representation into the format "\x00":
  7. octal = "\000\001\010\020"
  8. octal.each_byte { |x| puts x }
  9. # 0
  10. # 1
  11. # 8
  12. # 16

  13. hexadecimal = "\x00\x01\x10\x20"
  14. hexadecimal.each_byte { |x| puts x }
  15. # 0
  16. # 1
  17. # 16
  18. # 32
  19. This makes it possible to represent UTF-8 characters even when you can’t type them or display them in your terminal. Try running this program, and then opening the generated file smiley.html in your web browser:
  20. open('smiley.html', 'wb') do |f|
  21.   f << '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">'
  22.   f << "\xe2\x98\xBA"
  23. end
复制代码

7
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:55:29
  1. 2.6 Converting Between Characters and Values
  2. Problem
  3. You want to see the ASCII code for a character, or transform an ASCII code into a string.
  4. Solution
  5. To see the ASCII code for a specific character as an integer, use the String#ord method:
  6. "a".ord                 # => 97
  7. "!".ord                 # => 33
  8. "\n".ord                # => 10
  9. To see an individual character of a particular string, access it as though it were an element of an array:
  10. 'a'[0]                  # => "a"
  11. 'bad sound'[1]          # => "a"

  12. 'a'[0].ord              # => 97
  13. 'bad sound'[1].ord      # => 97
  14. To see the ASCII character corresponding to a given number, call its #chr method. This returns a string containing only one character:
  15. 97.chr              # => "a"
  16. 33.chr              # => "!"
  17. 10.chr              # => "\n"
  18. 0.chr               # => "\x00"
  19. 256.chr             # RangeError: 256 out of char range
复制代码

8
ReneeBK(未真实交易用户) 发表于 2016-11-29 09:56:44
  1. 2.7 Converting Between Strings and Symbols
  2. Problem
  3. You want to get a string containing the label of a Ruby symbol, or get the Ruby symbol that corresponds to a given string.
  4. Solution
  5. To turn a symbol into a string, use Symbol#to_s, or Symbol#id2name, for which to_s is an alias:
  6. :a_symbol.to_s                        # => "a_symbol"
  7. :AnotherSymbol.id2name                # => "AnotherSymbol"
  8. :"Yet another symbol!".to_s           # => "Yet another symbol!"
  9. You usually reference a symbol by just typing its name. If you’re given a string in code and need to get the corresponding symbol, you can use String.intern:
  10. :dodecahedron.object_id               # => 516488
  11. symbol_name = "dodecahedron"
  12. symbol_name.intern                    # => :dodecahedron
  13. symbol_name.intern.object_id          # => 516488
复制代码

9
Nicolle(未真实交易用户) 学生认证  发表于 2018-8-28 09:26:08
提示: 作者被禁止或删除 内容自动屏蔽

10
Nicolle(未真实交易用户) 学生认证  发表于 2018-8-28 09:28:25
提示: 作者被禁止或删除 内容自动屏蔽

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-9 03:09