Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Saturday, 14 September 2013

How to use/implement Array in Ruby

                                                 Array           

 

 Array is a collection of same "Data Type" or you can say it is a systematic arrangement of objects of same data type usually in rows and columns.

This tutorial describes "How Arrays can be created and manipulated in Ruby.

Programme  of array


irb(main):001:0> x=[1,2,3,4,5,6]
irb(main):001:0> Put x[2]

Output : 3

Pushing a String Information Array

irb(main):001:0> x=[ ]
x<<"Ruby"


Output :
["Ruby"]


Or

irb(main):001:0> x.push("Ruby")


PoP Information on Array

irb(main):001:0> x=[]
x<<"ruby"
x<<"is"
x<<"best"
put x
puts x.pop
puts x.pop
puts x.pop

Output
=> ["ruby","is","best"]
=>best
=>is
=>1


Join a String

irb(main):001:0> x=["Ruby"," is"," best"," friend"," for"," Hacker"]
irb(main):001:0> puts x.join

Output :
=>Ruby is best friend for Hacker


While loop in Array

irb(main):001:0>x=[1,"Ruby",2,3,4,5]
irb(main):001:0> i=0
irb(main):001:0> while (i<a.length)
  put a[i]. to_s+"x"
    i+=1
end

Output :
Try it

How to implement Interpolation in Ruby

                                  Interpolation


x=10
y=30
puts "#{x}+#{y}=#{x+y}"

Output :
10+30=40



You can put expression  even logic directly into string

puts "100*5=#{100*5}"

Output :
100*5=500

How to use String Manipulation in Ruby

                        String Manipulation


Print a string :

irb(main):001:0> Puts " this is ruby"
If you find out the length of string :
irb(main):001:0> Puts "this is ruby ".length
Use uppercase letter print on screen :
irb(main):001:0> Puts " this is ruby" .upcase

Method of String testing :

Expression                              O/P
"Ruby"+Ruby                        RubyRuby
"Ruby".capitalize                    Ruby
"Ruby".downcase                   ruby
"Ruby".upcase                        RUBY
"Ruby".chop                           Rub
"Ruby".next                            Rub
"Ruby".reverse                       ybuR
"Ruby".sum                   
"Ruby"_swapcase                  ybuR
"Ruby".up case.reverse          YBUR
"Ruby".up case.reverse.next   RUBZ

Friday, 13 September 2013

How to implement Frist Program in Ruby

                                      Programming

irb -> Interactive Ruby
Interactive means you type you computer it will immediately attempt to process
irb(main):001:0>
001 -> show line no I.e 1
0-> show depth I.e 0
irb(main):001:0>1+1
=>2
You performance all arithmetic (Mathematical operations) operation
Like: 100*3,146-899,464/899
Ruby is a special bcoz it like a neutral languages
irb(main):001:0> 100.times do print "Hello Ruby" end
Output:
It print 100 times Hello Ruby