if (nil == nil)
elsif boolean
else
end
check how elsif is written
Back
how to get the object id
Front
obj.object_id
Back
which method is used to increment a integer value
Front
.NEXT
457.next
Back
Ho to reverse an array
Front
arr.reverse
Back
how to reverse an array in place
Front
With a BANG after .reverse
arr.reverse!
Back
x = y || z
what does this mean
Front
Short hand for writing this is
if y
x = y
else
x = z
Back
remove nil from array
Front
arr.compact
arr.compact!
Back
write a while and until loop
Front
while boolean
end
----------
until boolean
end
Back
method to round a float
Front
round
floor - this will round to lowest value
ceil - this will round to ceiling number which is the greatest
Back
Find if something is nil
Front
product.nil?
product == nil
!product
Back
write a basic loop
Front
loop do
end
can use below inside a loop
redo - redo this loop
next - jump to next loop
break - terminate the whole loop
retry - start over the whole loop
Back
what does this short hand do
x ||= y
Front
if X has a value then nothings happens, else y is assigned
unless x
x = y
end
Back
If a array position is empty what will it return
Front
nil
Back
list iterators with hash class
Front
each_key
each_value
each_pair
Back
how many iterators are there and list them
Front
Total four
5.times {puts "hello"}
1.upto(5) { puts "Hello" }
5.downto(1) {puts "hello"}
(1..5).each { puts "hello" }
Back
Interpolation
Front
#{}
msg = 'I love you'
puts "I want to say that #{msg}"
Back
how to declare an array
Front
Arrays don't care about the object types
x = [1,2,3,4,5,'axczxc', nil]
Back
find the length of a string
Front
"swer".length
4
Back
string escaping
Front
'Let\'s escape'
Back
array delete a value
Front
arr.delete('3')
Back
Get all the keys in a hash
Front
car.keys
Back
how to join values in a array
Front
arr.join
arr.join(",")
Back
how to write a case statement
Front
case
when boolean
when boolean
end
OR
case count
when 0
puts "nobody"
when 1
puts "only one"
end
Back
Check if an array has an element
Front
arr.include?('venkat')
Back
array delete a value at a position
Front
arr.delete_at(4)
Back
Symbols are like a strings but cant be edited
begins with column
which integer method is used to convert an int to float and vice versa
Front
100.to_f
10.3.to_i
Back
how to shuffel an array
Front
arr.shuffle
arr.
Back
check if an array is empty
Front
[].empty?
Back
check if a hash has key and value
Front
hash.has_key?('fname')
hash.has_value('lname')
Back
what is the convention to declare a constant
Front
MAX_SCORE = 100
Ruby still allows to over write and gives a warning
Constants can also be started with capital letter
Person
Back
remove duplicates in an array
Front
arr.uniq
arr.uniq!
Back
how to flatten contents of array by
Front
[1,2, [3, 4], 5, 6].flatten
[1,2,3,4,5,6]
Back
list iterators with array class
Front
EACH,
each_index,
each_with_index
Back
To expect a result in float, what is a pre-requisite
Front
Atleast one number should be float
10/3 = 3
10.0/3 = 3.333335
Back
convert a hash to array
Front
hash.to_a
Back
how to use downto iterator
Front
5.downto(1) do |G|
puts "countdown -- #{G}"
end
Back
list iterators by range
Front
each, step
Back
Whats spacility in hash
Front
we can relay on position
they are object indexed instead of position indexed like arrays
It's like key value pair
car = {
'brand' => 'ford',
'model' => 'mustand'
'color' => 'blue',
'interier_color' => 'tan'
}
car['brand']
car['price'] = '29,403'
Back
what does find do ?
Front
(1..10).find {|n| n == 5 }
It returns the matched condition
Back
Section 2
(11 cards)
how to add get/set methods in ruby
Front
attr_accessors
attr_accessor :noise, :color
Back
when are merge methods needed
Front
Merge methods are used in the case of hashes
Used to combine two hashes and make them one
h1 = {:a => 2, :b => 4, :c => 6}
h2 = {:a => 3, :b => 4, :c => 6}
h1.merge(h2) #
or
In the case of conflict
h1.merge(h2) { | k, o, n | o*n }
Back
delete array contents of they met a condition
Front
numbers = [*1..10]
numbers.delete_if { |n| n <= 5 }
in a map total number of elements in input will always be equal to output
x = [1, 2, 3, 4, 5]
y = x.map {|n| n+1}
similer to JS map
Back
Why is inject method used
Front
It accumelates values and remembers them. It is like reduce in JS
(1..5).inject { |memo, n| memo + n }
-----Find the length of all the strings
fruits = ['apple', 'banana']
fruits.inject(0) do |memo, fruit|
memo + fruit.length
end
Back
what does find_all do ?
Front
(1..10).find_all {|n| n%3 == 0}
Filter equivalent in JS
Back
how to perform a sort
Front
array = [5,2,3,6,3,2,56,2]
x = array.sort{ |v1, v2| v1 <=> v2 } #ascending order
x = array.sort{ |v1, v2| v2 <=> v1 } #descending order