i = 0 while i < 5 do puts i i += 1 end
#!/usr/bin/ruby class Sample def hello helloWorld = "Hello, World!" helloWorld end end # Now using above class to create objects object = Sample. new object.hello
A great resource for seeing the Ruby Array methods.
ary = [1, "two", 3.0] #=> [1, "two", 3.0] ary = Array.new #=> [] Array.new(3) #=> [nil, nil, nil] Array.new(3, true) #=> [true, true, true] Array.new(4) { Hash.new } #=> [{}, {}, {}, {}] Array.new(4) {|i| i.to_s } #=> ["0", "1", "2", "3"] empty_table = Array.new(3) { Array.new(3) } # => [nil, nil, nil], [nil, nil, nil], [nil, nil, nil](/ruby/nil, nil, nil], [nil, nil, nil], [nil, nil, nil)
arr = [1, 2, 3, 4, 5, 6] arr[2] #=> 3 arr[100] #=> nil arr[-3] #=> 4 arr[2, 3] #=> [3, 4, 5] arr[1..4] #=> [2, 3, 4, 5] arr[1..-3] #=> [2, 3, 4] arr.at(0) #=> 1
arr = ['a', 'b', 'c', 'd', 'e', 'f'] arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6 arr.fetch(100, "oops") #=> "oops"
# Examples a.gsub(/[^a-z]/i, "").downcase.chars.sort.join! # update variable a