Ruby Tutorial
Ruby CGI
Ruby Advanced
In Ruby, a method is a reusable block of code that performs a specific task. Methods can be defined with the def
keyword and are invoked by their name.
Here is a basic tutorial on how to define and use methods in Ruby:
1. Defining a Method:
Here is the basic syntax for defining a method:
def say_hello puts "Hello, world!" end
In this example, say_hello
is the method name. You can call this method using its name:
say_hello # Outputs: Hello, world!
2. Method Parameters:
Methods can also take parameters, which are values you can pass into the method when you call it:
def greet(name) puts "Hello, #{name}!" end greet("Alice") # Outputs: Hello, Alice!
In this example, name
is a parameter. When you call greet("Alice")
, the string "Alice" is passed into the method, and it gets assigned to the name
parameter.
3. Default Parameters:
You can provide default values for parameters. If an argument for that parameter is not provided when the method is called, the default value will be used:
def greet(name = "world") puts "Hello, #{name}!" end greet("Alice") # Outputs: Hello, Alice! greet # Outputs: Hello, world!
4. Return Values:
Methods in Ruby automatically return the value of the last expression. For example:
def square(number) number * number end result = square(3) puts result # Outputs: 9
In this example, the square
method returns the result of number * number
. You can also use the return
keyword to return a value early, but this is not commonly done in Ruby.
5. Variable Number of Arguments:
If you prefix a parameter with an asterisk (*
), any number of arguments can be passed to the method:
def greet(*names) names.each do |name| puts "Hello, #{name}!" end end greet("Alice", "Bob", "Charlie") # Outputs: Hello, Alice! Hello, Bob! Hello, Charlie!
In this example, *names
is a parameter that can take any number of arguments. The arguments are passed in as an array.
These are the basics of methods in Ruby. There are more advanced features like method overloading, private methods, etc., that you can explore as you get more comfortable with the basics.
Defining methods in Ruby:
Methods in Ruby are defined using the def
keyword.
def greet(name) puts "Hello, #{name}!" end
Method parameters in Ruby: Parameters are variables that receive values when the method is called.
def add(a, b) result = a + b puts result end
Default values for method parameters in Ruby: You can set default values for parameters.
def greet(name = 'Guest') puts "Hello, #{name}!" end
Ruby method return values: Methods automatically return the value of the last expression.
def square(x) x * x end
Ruby method arguments: Methods can take a variable number of arguments.
def multiply(*numbers) result = 1 numbers.each { |num| result *= num } puts result end
Method overloading in Ruby: Ruby doesn't support traditional method overloading. Instead, you can use default values or variable arguments.
def add(a, b = 0) a + b end
Private and public methods in Ruby:
Methods are public by default. Use private
to restrict access.
class MyClass def public_method # ... end private def private_method # ... end end
Ruby method chaining: Method chaining involves calling multiple methods in a single line.
result = array.select(&:even?).map { |num| num * 2 }.sum
Dynamic method creation in Ruby: Methods can be dynamically defined at runtime.
class MyClass define_method(:dynamic_method) do puts "Dynamic method!" end end
Ruby class methods vs. instance methods: Class methods are defined on the class, while instance methods are defined on instances of the class.
class MyClass def instance_method # ... end def self.class_method # ... end end
Ruby alias method:
The alias
keyword creates an alias for a method.
class MyClass def original_method puts "Original method" end alias new_method original_method end
Ruby method visibility:
Use public
, private
, and protected
to control method visibility.
class MyClass def public_method # ... end private def private_method # ... end end