MainMenu

Home Core Java Overview Maven Tutorials

Sunday, 26 August 2018

Basic program in ruby 2

Basic Programming in ruby Array, String, Methods



Hello Friends,
We will learn more about ruby programming that how to create Array, Access elements from array , String & methods


0). How to create an Array in Ruby :-

arr1 =Array.[](1,3,5,7,9)
puts arr1



Output :-

1
3
5
7
9


----------------------------------------------------
1). How to create an Array in Ruby by second way :-

arr2 = Array[2,4,6,8]
puts arr2



2
4
6
8
-------------------------------------------------------

2). How to get length an Array in Ruby :-

arr = Array.new(10)
puts array.length
puts arr.size
puts arr.length


Output :-

10
10


--------------------------------------------------------------
3). How to create a string Array in Ruby :-

arr3 = Array["chandan", "shina", "adhiraj", 2, 46, "akshat", "sarvakaram"]
puts arr3[2..5]


Output :-
adhiraj
2
46
akshat

--------------------------------------------------------------
4). String in Ruby :-

str = "chandanSingh"
puts str.capitalize
puts str.upcase
puts str.downcase
puts str.reverse
puts str.length
puts str[2]
puts str[2..4]


Output :-
Chandansingh
CHANDANSINGH
chandansingh
hgniSnadnahc
12
a
and

--------------------------------------------------------------
5). Methods in Ruby :-

def add(x, y)
puts x+y
end
add 10, 20
def language(a, b)
puts "best language for beginner is #{a}"
puts "second best language for beginner is #{ b}"
end
language "Ruby", "Core java"
def name(x= "chandan", y ="Shreya")
puts "my name is #{x}"
puts "my friend name is #{y}"
end
name
name "adhiraj" , "ritu"



Output :-
30
best language for beginner is Ruby
second best language for beginner is Core java
my name is chandan
my friend name is Shreya
my name is adhiraj
my friend name is ritu


--------------------------------------------------------------
6). How to pass multiple parameter in methods in Ruby :-

def multipara(*test)
puts "length of parameter is #{test.length}"
for i in 0..test.length-1
puts "Elements of parameter is #{test[i]}"
end
end
multipara "chandan", "Shina", "arti" ,"reena"





Output :-
length of parameter is 4
Elements of parameter is chandan
Elements of parameter is Shina
Elements of parameter is arti
Elements of parameter is reena



Next Topic :- How to configure cucumber in Ruby Mine

Previous Topic :- Basic Programming in ruby for, if-else, while do-while loop etc

No comments:

Post a Comment