Ruby Built-in Methods

Powerful pre-defined methods for common operations

🛠️ What are Built-in Methods?

Ruby built-in methods are pre-defined functions that perform common operations on objects. They save time and make code more readable by providing ready-to-use functionality for strings, arrays, numbers, and more.


# Built-in methods make coding easier
name = "ruby"
puts name.upcase
puts name.length
                                    

Output:

RUBY
4

Method Categories

📝

String Methods

Manipulate and transform text

upcase, downcase, capitalize
reverse, length, include?
📊

Array Methods

Work with collections of data

push, pop, shift, unshift
each, map, select, sort
🔢

Numeric Methods

Perform mathematical operations

abs, round, ceil, floor
even?, odd?, times
🗂️

Hash Methods

Manage key-value pairs

keys, values, has_key?
merge, delete, each

🔹 String Methods

String methods help you manipulate text data. Ruby provides numerous built-in methods for transforming, searching, and analyzing strings. These methods are essential for text processing and user input handling.

# Case conversion
text = "Hello World"
puts text.upcase
puts text.downcase
puts text.capitalize
puts text.swapcase

# String information
puts text.length
puts text.include?("World")
puts text.start_with?("Hello")
puts text.end_with?("!")

# String manipulation
puts text.reverse
puts text.gsub("World", "Ruby")
puts "  spaces  ".strip

Output:

HELLO WORLD
hello world
Hello world
hELLO wORLD
11
true
true
false
dlroW olleH
Hello Ruby
spaces

🔹 Array Methods

Array methods allow you to work with collections efficiently. You can add, remove, search, and transform array elements using these powerful built-in methods. They are fundamental for data manipulation in Ruby.

# Adding and removing elements
fruits = ["apple", "banana"]
fruits.push("orange")
fruits << "grape"
puts fruits.inspect

last = fruits.pop
puts "Removed: #{last}"
puts fruits.inspect

# Array operations
numbers = [3, 1, 4, 1, 5, 9]
puts numbers.sort.inspect
puts numbers.reverse.inspect
puts numbers.uniq.inspect
puts numbers.max
puts numbers.min
puts numbers.sum

# Searching
puts numbers.include?(4)
puts numbers.index(4)

Output:

["apple", "banana", "orange", "grape"]
Removed: grape
["apple", "banana", "orange"]
[1, 1, 3, 4, 5, 9]
[9, 5, 1, 4, 1, 3]
[3, 1, 4, 5, 9]
9
1
23
true
2

🔹 Numeric Methods

Numeric methods perform mathematical operations and checks on numbers. Ruby provides methods for rounding, absolute values, and checking number properties. These methods simplify mathematical calculations in your programs.

# Basic math methods
num = -15.7
puts num.abs
puts num.round
puts num.ceil
puts num.floor

# Number checks
puts 10.even?
puts 10.odd?
puts 7.even?
puts 7.odd?

# Number operations
puts 5.times { |i| print "#{i} " }
puts ""
puts 3.upto(6) { |i| print "#{i} " }
puts ""
puts 10.downto(7) { |i| print "#{i} " }

Output:

15.7
-16
-15
-16
true
false
false
true
0 1 2 3 4 
3 4 5 6 
10 9 8 7

🔹 Hash Methods

Hash methods help you work with key-value pairs. Hashes are like dictionaries where you store data with unique keys. These methods let you access, modify, and query hash data efficiently.

# Creating and accessing hashes
person = { name: "Alice", age: 25, city: "NYC" }
puts person[:name]
puts person[:age]

# Hash information
puts person.keys.inspect
puts person.values.inspect
puts person.has_key?(:name)
puts person.has_value?(25)
puts person.length

# Hash operations
person[:country] = "USA"
puts person.inspect

person.delete(:city)
puts person.inspect

# Merging hashes
extra = { job: "Developer" }
full_info = person.merge(extra)
puts full_info.inspect

Output:

Alice
25
[:name, :age, :city]
["Alice", 25, "NYC"]
true
true
3
{:name=>"Alice", :age=>25, :city=>"NYC", :country=>"USA"}
{:name=>"Alice", :age=>25, :country=>"USA"}
{:name=>"Alice", :age=>25, :country=>"USA", :job=>"Developer"}

🔹 Common Object Methods

All Ruby objects inherit common methods from the Object class. These universal methods work on any object and provide essential functionality like type checking, conversion, and inspection.

# Type checking
puts 42.class
puts "hello".class
puts [1, 2, 3].class
puts true.class

# Type conversion
puts "123".to_i
puts 456.to_s
puts "3.14".to_f
puts [1, 2, 3].to_s

# Object inspection
value = nil
puts value.nil?
puts 10.nil?

# Respond to method
puts "hello".respond_to?(:upcase)
puts 42.respond_to?(:upcase)

# Object ID
puts "test".object_id

Output:

Integer
String
Array
TrueClass
123
456
3.14
[1, 2, 3]
true
false
true
false
60

🧠 Test Your Knowledge

Which method converts a string to uppercase?