This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require ‘./employee.rb’ | |
e = Employee.new | |
e.say_hello(“Michael”) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2/0 # => ZeroDivisionError | |
# In order to give the user a better error message than "ZeroDivisionError" | |
# we have to wrap the code using begin & rescue keywords so that Ruby knows | |
# that we specifically want to do something if an error is encountered | |
begin | |
2/0 | |
rescue | |
“You can’t divide by ZERO!” |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_result(number) | |
if(number % 3 == 0 && number % 5 == 0) | |
return "FizzBuzz" | |
elsif(number % 3 == 0) | |
return "Fizz" | |
elsif(number % 5 == 0) | |
return "Buzz" | |
else | |
return number.to_s | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts “Enter your name:” | |
name = gets.chomp | |
puts “Hello, #{name}!” | |
## Output | |
# PROMPT> ruby hello.rb | |
# Enter your name: | |
# Michael | |
# Hello, Michael! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic Module | |
module MyModule | |
def module_hello | |
return “MyModule module_hello method” | |
end | |
end | |
# Class with module included | |
class ClassOne | |
include MyModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee | |
attr_accessor :name, :age | |
attr_reader :salary, :department | |
initialize(name = “”, salary = 0, department = “”) | |
@name = name | |
@salary = salary | |
@department = department | |
end | |
def set_new_salary(salary) | |
@salary = salary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee | |
attr_accessor :name | |
def self.new_with_name(name) | |
output = self.new | |
output.name = name | |
return output | |
end | |
end | |
# List all methods on the employee class and sort them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Here is a basic class, with a basic | |
## initialize method that takes a name | |
## argument | |
class Employee | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This is a basic Ruby class | |
class Employee | |
end | |
## We can create an object from this class | |
e = Employee.new # => #<Employe:0x37978873> #Or some other gibberish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Basic method with multiple arguments | |
def add_numbers(a, b) | |
a + b | |
end | |
add_numbers(5, 4) # => 9 |