Tuesday, March 31, 2015

Hands on Ruby Numbers and Arrays

#Ruby can handle both integars and floating points
 #we can add this putting a # before a variable prints 
puts "twenty + one_point_two = #{twenty+one_point_two}"
puts 4+4.0
#puts 4+"Ajay"  #   <=== Try this you will get an error
you can use an underscore as a thousands divider when writing long numbers; Ruby ignores the underscore. 
This makes it easy to read large numbers:
puts billion
#creating an Array
first_array = []   #empty array
second_array = Array.new #empty array
third_array =[1,2,3]
puts third_array
second_array[0]="Different ways of adding an object to array"
puts first_array
puts second_array
fourth_array = Array.new(20) #created an array of size 20
puts fourth_array.size
puts fourth_array.length
names = Array.new(4, "Ajay")  #names has 4 objects
puts "#{names}"
puts names.length


Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment