Ruby中的星号(*)

Tuesday, July 20, 2010

Ruby中的星号 (*)

>> match, text, number = *"Something 981".match(/([A-z]*) ([0-9]*)/)
=> ["Something 981", "Something", "981"]
>> a, b, c = *('A'..'Z')
=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
>> a
=> "A"
>> b
=> "B"
>> c
=> "C"
>> Job = Struct.new(:name, :occupation)
=> Job
>> tom = Job.new("Tom", "Developer")
=> #
>> name, occupation = *tom
=> ["Tom", "Developer"]
>> name
=> "Tom"
>> occupation
=> "Developer"

This entry was tagged hidden-features and Ruby

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top