Ruby-Refresher

Rails makes me stupid with Ruby. These are the Ruby Basics. Maybe even Programming Basics. I always seem to forget because someone is always doing it for me…except when they are not…

irb -> ruby console…Great! I love u irb. But rails c is your superior because it automagically loads the entire fucking environment into rails c. While irb just leaves me out in the cold naked without any of my files loaded in…

irb loading files

Let’s say I have a the following file structure lib/ruby_file.rb and I want to load that file.

$ irb
> load 'lib/ruby_file.rb'

That will load hand_evaluator.rb… Like a boss… ALSO load will load the file even after you’ve changed it. http://stackoverflow.com/questions/4632090/how-can-i-reload-a-script-in-irb for more information. It really depends on where you start irb, if it is in the right directory, then it should work with a relative routing

Example Gemfile

source "https://rubygems.org"
ruby "2.2.4"
gem 'rspec'

classes within a Ruby File

Things within a Ruby File should be in a class.

class Rabbit
  def have_babies
# Do the nasty
  end
end

Once I’ve loaded that into irb, I can then call Rabbit.new.have_babies.

Ruby Classes in a Rails Application

I am just learning this… I can make a Ruby class, save it into my app/models/, but not have it inherit from ActiveRecord::Base. For instance, if I just want to parse out some functionality into it’s own class, but don’t need an entire backend db, I can go ahead and create a Ruby class and save it into my models directory.

to do this… save the file as rabbit.rb

class Rabbit
  def initialize(name, age)
    @name = name
    @age = age
  end

  def can_have_babies?
if @age > 0
  true
else
  false
end
  end
end

OR IF YOU WANT AN ATTRIBUTES HASH

class Rabbit
  def initialize(attributes = {})
@name = attributes[:name]
@age = attributes[:age]
  end

  def can_have_babies?
@age > 1 ? true : false
  end

  def is_celebrity?
@name == "Miley" ? true : false
  end
end

USAGE…

miles = Rabbit.new(name:"Miley", age:3)
miles.can_have_babies?
miles.is_celebrity?

MORE CLASS EXAMPLES

class Mortgage
  def initialize(loan_amount, interest_rate, term_in_months)
    @loan_amount = loan_amount
    @interest_rate = interest_rate
    @term_in_months = term_in_months
  end

  def monthly_payment(loan_amount: @loan_amount, interest_rate: @interest_rate, term_in_months: @term_in_months)
    monthly_rate = interest_rate / 12.0

    loan_amount * ((monthly_rate * (1 + monthly_rate)**term_in_months)/((1 + monthly_rate)**term_in_months - 1))
  end

  def lifetime_payment_amount
    self.monthly_payment * @term_in_months
  end

  def total_interest_paid
    self.lifetime_payment_amount - @loan_amount
  end

end
my_mort = Mortgage.new(100000, 0.05, 360) 

Loading Gems into irb

One gem I always want to load is byebug.

require byebug should work if you have the gem installed in your gemspec. But sometimes it isn’t, then you’ll need to add it into your Gemfile and run bundle.

Gemfile gem byebug

then run … $ bundle install

Post Content