Methods

def some_method
   # body omitted
 end

 def some_method_with_parameters(arg1, arg2)
   # body omitted
 end

String Interpolation

Interpolation will only work on Strings wrapped in double quotes " ". Single quotes: ' ' do not support string interpolation.

answer = "You are!"
puts "Whos the man? #{answer}."
# => Whos the man? You are!

String Interpolation inside a method:

def greeting(name)
  puts "Hello, #{name}!"
end

Adding default arguments

def greeting(name="Marcus", job="Developer")
  puts "Hello, #{name}. We heard you are a great #{job}."
# => Hello Marcus. We heard you are a great Developer.
end

Rspec and TDD

The basic idea behind TDD is that you should think about what you want your program to do and how you want your code to behave before you start coding. Especially as you begin to write more complex programs or develop applications, bringing this mindfulness to your development process will help you to write code that is robust (doesn't break all the time), flexible (accommodates future change and growth) and easy for other developers to understand.

require_relative '../current_age_for_birth_year.rb'

describe "current_age_for_birth_year method" do
  it "returns the age of a person based on the year of birth" do
    age_of_person = current_age_for_birth_year(1984)

    expect(age_of_person).to eq(32)
  end
end

results matching ""

    No results matching ""