[Ruby笔记]13.Ruby object .replace("") .dup .freeze

.replace()

  • 打开irb
PS C:\Users\Administrator> irb --simple-prompt
  • 创建字符串变量str ,值为“hello”,赋值给变量abc,本质是使abc指向同一个字符串对象,所以均输出为同一个“hello”
>> str = "Hello"
=> "Hello"
>> abc = str
=> "Hello"
  • str 变量使用.replace(""),使其指向的object的值修改为"Goodbye",直接导致指向同一个对象的abc也会输出“Goodbye”
>> str.replace("Goodbye")
=> "Goodbye"
>> puts str
Goodbye
=> nil
>> puts abc
Goodbye
=> nil

.dup and .freeze

创建修改字符串方法change_string()

PS C:\Users\Administrator> irb --simple-prompt
>> def change_string(str)
>>     str.replace("New String")
>> end
=> :change_string

example

  • 被改变成新的字符串
>> s = "Hello world"
>> change_string(s)
>> puts s
New String 
  • .dup(duplicate)保持原来的值没有变化
>> s = "Hello world"
>> change_string(s.dup) # 对s使用.dup相当于创建了一个副本
>> puts s
Hello world  
  • can’t modify frozen String
>> s = "Good Day"
>> s.freeze
>> change_string(s)
RuntimeError: can't modify frozen String

freeze and replace

  • numbers使用.freeze,使得无法对numbers进行修改操作 :

PS C:\Users\Administrator> irb
irb(main):001:0> numbers = ["one","two","three"]
=> ["one", "two", "three"]

irb(main):002:0> numbers.freeze
=> ["one", "two", "three"]


irb(main):005:0> numbers[2] = "four"
RuntimeError: can't modify frozen Array
        from (irb):5
        from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
  • 但是仍可以用.replace修改numbers中的元素"three"指向的对象的值 :
irb(main):006:0> numbers[2].replace("four")

irb(main):007:0> numbers
=> ["one", "two", "four"]

reference

《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
2.5. Local variables and variable assignment 2.5.1~2.5.3

   ∧_∧
  (・д・ )
 ¬(  )ゝ
 .../ 〈

http://emoji.vis.ne.jp/hiiteshimauna2.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Computer savvy kids and can be found surfing the web, chatting with friends, downloading music, and playing games - all at the same time. Many, however, want to do more. Not only do they want to use computers to stay connected to the world, they want to understand what makes them work, and they want to make computers work for them; they want to learn programming. Warren and Carter Sande, a father and son team, created just the book to get them started. Hello World! Second Edition is a fun, easy-to-use guide with copious illustrations and engaging examples. It takes the reader on a playful tour of basic programming concepts and then puts those concepts together to make fun and useful programs. It uses Python, a programming language that is one of the easiest to read, write, and understand. Like the first edition, Hello World! Second Edition is not just for kids. While the tone is light and engaging, it doesn't "talk down" to the reader, and beginners of any age will love its readability and sense of humor. Uses free open source software Makes programming fun and easy to use Contains lots of pictures, cartoons, and fun examples Hello World! Second Edition is printed in full color Readers don't have to have any programming experience, but they should know the basics of using a computer. This book uses Python version 2.7.3. It's free open source software available for different platforms, including Windows, Linux, and Mac. Python is a programming language that is one of the easiest to read, write, and understand. Python is not just for beginners though. It used by programmers at places like NASA and Google, so once learned, it's a language that can expand to match the novice programmer's growing expertise. It also serves as a great foundation for learning many other programming languages.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值