info@webounstraininghub.in
Ruby is a dynamic, object-oriented programming language known for its simplicity and productivity.
Features include dynamic typing, automatic memory management, rich libraries, and support for multiple programming paradigms.
==
checks for value equality, while equal?
checks for object identity (whether both operands are the same object).
Blocks are chunks of code passed to methods and can be used to iterate over collections or perform operations.
yield
is used to pass control from a method to a block of code.
A Ruby Gem is a packaged Ruby application or library that can be distributed and installed using the RubyGems package manager.
A module is a collection of methods, constants, and classes that can be included or extended in other classes to share functionality.
A class in Ruby is a blueprint for creating objects, defining their properties and behaviors.
Inheritance allows a class to inherit attributes and methods from another class, promoting code reuse.
A hash is a collection of key-value pairs, where each key is unique, and it is used to store and retrieve data efficiently.
attr_accessor
creates getter and setter methods for instance variables in a class.
Ruby handles exceptions using begin
, rescue
, ensure
, and raise
to manage errors and ensure proper handling.
Symbols are lightweight, immutable, and unique identifiers often used as keys in hashes or for method names.
include
adds methods from a module as instance methods, while extend
adds them as class methods.
The Enumerable
module provides a set of methods for collection classes that allow iteration and searching.
Methods are defined using the def
keyword, followed by the method name and optional parameters, ending with end
.
A Proc
is an object that encapsulates a block of code, allowing it to be stored and executed later.
self
refers to the current instance of a class or module, allowing access to instance methods and variables.
Duck typing allows objects to be used based on their behavior rather than their explicit type, as long as they implement the required methods.
A singleton method is a method defined for a single object rather than for all instances of a class.
Classes are blueprints for objects, which are instances of classes that encapsulate data and behavior.
Ruby uses automatic garbage collection to manage memory by removing objects that are no longer referenced.
A class can be instantiated to create objects, while a module cannot; modules are used to group methods and constants.
Enumerable
provides a set of methods for collection classes, including map
, select
, reduce
, and each
.
Constants are defined using uppercase names and are typically defined outside of methods or classes.
def even_or_odd(num)
num.even? ? "Even" : "Odd"
end
puts even_or_odd(4) # Output: Even
def factorial(n)
(1..n).reduce(1, :*)
end
puts factorial(5) # Output: 120
def reverse_string(str)
str.reverse
end
puts reverse_string("Hello") # Output: olleH
def palindrome?(str)
str == str.reverse
end
puts palindrome?("madam") # Output: true
def swap(a, b)
a, b = b, a
[a, b]
end
a, b = swap(5, 10)
puts "a = #{a}, b = #{b}" # Output: a = 10, b = 5
def find_largest(arr)
arr.max
end
puts find_largest([2, 5, 1, 9, 7]) # Output: 9
def remove_duplicates(arr)
arr.uniq
end
puts remove_duplicates([1, 2, 2, 3, 4, 4, 5]).inspect # Output: [1, 2, 3, 4, 5]
def count_characters(str)
str.chars.tally
end
puts count_characters("hello").inspect # Output: {"h"=>1, "e"=>1, "l"=>2, "o"=>1}
def sort_array(arr)
arr.sort
end
puts sort_array([4, 2, 8, 5, 1]).inspect # Output: [1, 2, 4, 5, 8]
def sum_array(arr)
arr.sum
end
puts sum_array([1, 2, 3, 4, 5]) # Output: 15
def contains_word?(str, word)
str.include?(word)
end
puts contains_word?("Hello world", "world") # Output: true
def random_string(length = 10)
Array('a'..'z').sample(length).join
end
puts random_string # Example Output: xylmnpzjda
function stringLength($str) {
return strlen($strdef string_length(str)
str.length
end
puts string_length("Hello World") # Output: 11
);
}
echo stringLength("Hello World"); // Output: 11
def leap_year?(year)
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
end
puts leap_year?(2024) # Output: true
def count_vowels(str)
str.count("aeiouAEIOU")
end
puts count_vowels("Hello World") # Output: 3
def merge_arrays(arr1, arr2)
arr1 + arr2
end
puts merge_arrays([1, 2, 3], [4, 5, 6]).inspect # Output: [1, 2, 3, 4, 5, 6]
def prime?(num)
return false if num <= 1
(2..Math.sqrt(num)).none? { |i| num % i == 0 }
end
puts prime?(7) # Output: true
def capitalize_words(str)
str.split.map(&:capitalize).join(' ')
end
puts capitalize_words("hello world") # Output: Hello World
def to_lowercase(str)
str.downcase
end
puts to_lowercase("HELLO WORLD") # Output: hello world
def array_intersection(arr1, arr2)
arr1 & arr2
end
puts array_intersection([1, 2, 3, 4], [3, 4, 5, 6]).inspect # Output: [3, 4]
def gcd(a, b)
b.zero? ? a : gcd(b, a % b)
end
puts gcd(12, 15) # Output: 3
def to_uppercase(str)
str.upcase
end
puts to_uppercase("hello world") # Output: HELLO WORLD
def empty_array?(arr)
arr.empty?
end
puts empty_array?([]) # Output: true
def remove_last_element(arr)
arr[0...-1]
end
puts remove_last_element([1, 2, 3, 4]).inspect # Output: [1, 2, 3]
def word_count(str)
str.downcase.split.each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 }
end
puts word_count("hello world hello").inspect # Output: {"hello"=>2, "world"=>1}
def fibonacci(n)
sequence = [0, 1]
while sequence.last < n
sequence << sequence[-1] + sequence[-2]
end
sequence
end
puts fibonacci(10).inspect # Output: [0, 1, 1, 2, 3, 5, 8]
def min_max(arr)
[arr.min, arr.max]
end
puts min_max([2, 5, 1, 9, 7]).inspect # Output: [1, 9]
def sum_even(arr)
arr.select(&:even?).sum
end
puts sum_even([1, 2, 3, 4, 5, 6]) # Output: 12
require 'date'
def string_to_date(date_str)
Date.parse(date_str)
end
puts string_to_date("2024-09-01") # Output: 2024-09-01
def range_with_step(start, stop, step)
(start..stop).step(step).to_a
end
puts range_with_step(1, 10, 2).inspect # Output: [1, 3, 5, 7, 9]
We provide expert guidance, personalized support, and resources to help you excel in your digital marketing career.
Timing
9:00 am - 5:00 pm
info@webounstraininghub.in