Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents

Arrays are like Python's List type. There is no tuple type in Ruby, but you can freeze an array for the same effect.


 mylist = [1,2,3,4,5] mytuple = mylist.freeze myslice = mylist[0...3] # [1,2,3] 

As you can see, specifying a slice in Ruby using a Range with ... is like using : in Python. You must include both the beginning and the end. It is actually more common to use .. and specify the list inclusively. There is no direct equivalent to Python's ellipsis or list comprehension syntax.

Use <code>include?</code> where you would have used a conditional <code>in</code> in Python.


 if mylist.include? 4    puts "Found." end 

Use the +, <<, or push methods from the Array class to accomplish list.append functionality.




The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: