#Ruby can handle both integars and floating points
twenty=20
one_point_two=1.2
#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
=begin
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:
=end
billion=1_000_000_000
puts billion
#creating an Array
first_array = [] #empty array
second_array = Array.new #empty array
third_array =[1,2,3]
puts third_array
first_array.push("Ajay")
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.
Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts
Tuesday, March 31, 2015
Hands on Ruby Numbers and Arrays
Hands on ruby:Install,run,hello world
1.Install from Terminal
sudo apt-get install ruby2.0 ruby2.0-dev
2.How to run a program? There are two ways to do it.
a.Interactive ruby shell Open your terminal(Ctrl+alt+t)
Type irb and hit enter.
Voila you can start typing your commands here
b.Type all your commands and save it as filename.rb
open your terminal ruby filename.rb
3.Every thing in ruby is an object including numbers
sudo apt-get install ruby2.0 ruby2.0-dev
2.How to run a program? There are two ways to do it.
a.Interactive ruby shell Open your terminal(Ctrl+alt+t)
Type irb and hit enter.
Voila you can start typing your commands here
b.Type all your commands and save it as filename.rb
open your terminal ruby filename.rb
3.Every thing in ruby is an object including numbers
puts "Hello World"
puts "puts is like \n 1.printf in C \n 2. cin in C++ \n 3.System.out.print in java \n4.print in Python\n"
puts " # is a Single line comment"
=begin
This is multiline comment
Every thing in between =begin and =end
is a multi line comment
=end
languagge = "Ruby" #variable languagge has a value Ruby
puts "Hello #{languagge}"
puts 4 #Number is an object in ruby
puts 4+4
puts 4.class
#puts 4.methods # Displays all methods that you can call on a number
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.
Subscribe to:
Posts (Atom)

