The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)

 

   

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.


   
 

Категории