prisskreative
1/29/2015 - 12:11 AM

Ruby Iron Hack Work

Ruby Iron Hack Work

class Validator
  def initialize(user)
    @user = user
  end
 
  def is_valid?
    @user.token_value && Date.today > @user.token_expiration
  end
end
 
 
# Bad
#---------
class User
  # User has too much stuff for Validator
  attr_accessor :token_expiration, :token_value, :name, :email
 
  # ...
end
 
user = User.new
validator = Validator.new(user)
 
 
# Good
#---------
class User
  attr_accessor :token, :name, :email
 
  def intitialize(token)
    @token = token
  end
end
 
class Token
  attr_accessor :token_expiration, :token_value
end
 
token = Token.new
user = User.new(token)
validator = Validator.new(user.token)
class Authenticator
  def initialize(username, password)
    # @credentials = credentials
    @username = username
    @password = password
  end
 
  def valid?(given_username, given_password)
    # match = @credentials.find do |creds|
 
    # end
 
    return given_username == @username && given_password == @password
  end
end
 
 
class WordThing
  def initialize(input)
    @text = input
  end
 
  def word_count
    return @text.split(' ').size
  end
end
 
 
class WordThingTheSequel < WordThing
  def letter_count
    return @text.size
  end
 
  def reverse
    return @text.reverse
  end
 
  def upper
    return @text.upcase
  end
 
  def lower
    return @text.downcase
  end
end
 
 
# credentials = [
#   { username: 'nizar', password: 'swordfish' },
#   { username: 'whatevs', password: 'password' }
# ]
auth = Authenticator.new('nizar', 'swordfish')
 
puts 'Whats the username?'
username = gets.chomp
 
puts 'Whats the password?'
password = gets.chomp
 
if !auth.valid?(username, password)
  puts 'You messed up....'
else
  puts ''
  puts 'Good stuff. Now give me some words:'
  input = gets.chomp
  word_thing = WordThingTheSequel.new(input)
 
  puts 'What would you like to do with the words?'
  puts '[words] count words'
  puts '[letters] count letters'
  puts '[reverse] reverse text'
  puts '[upper] convert to uppercase'
  puts '[lower] convert to lowercase'
 
  option = gets.chomp
 
  case option
  when 'words'
    puts "Your word count is: #{word_thing.word_count}"
  when 'letters'
    puts "Your letter count is: #{word_thing.letter_count}"
  when 'reverse'
    puts "Your text reversed is: #{word_thing.reverse}"
  when 'upper'
    puts "Your text UPPERCASE is: #{word_thing.upper}"
  when 'lower'
    puts "Your text LOWERCASE is: #{word_thing.lower}"
  else
    puts 'You didnt choose the right option'
  end
end
class Authenticator
  def initialize(username, password)
    # @credentials = credentials
    @username = username
    @password = password
  end
 
  def valid?(given_username, given_password)
    # match = @credentials.find do |creds|
 
    # end
 
    return given_username == @username && given_password == @password
  end
end
 
 
class WordThing
  def initialize(input)
    @text = input
  end
 
  def word_count
    return @text.split(' ').size
  end
end
 
 
# credentials = [
#   { username: 'nizar', password: 'swordfish' },
#   { username: 'whatevs', password: 'password' }
# ]
auth = Authenticator.new('nizar', 'swordfish')
 
puts 'Whats the username?'
username = gets.chomp
 
puts 'Whats the password?'
password = gets.chomp
 
if !auth.valid?(username, password)
  puts 'You messed up....'
else
  puts ''
  puts 'Good stuff. Now give me some words:'
  input = gets.chomp
 
  word_thing = WordThing.new(input)
  puts "Your word count is: #{word_thing.word_count}"
end
class Car
  # Add sound to the initializer
  def initialize(brand, sound)
    @brand = brand
    @sound = sound
  end
 
  def noise
    puts @sound
  end
 
  # Add pretty string represention
  def to_s
    return "Brand: #{@brand}, Sound: #{@sound}"
  end
end
 
# Start with an array of sounds
sounds = [ 'vroooom', 'put put put', 'chug chug chug' ]
 
# Map over the array of sounds and create cars from them
cars = sounds.map do |sound|
  Car.new('Toyota', sound)
end
 
# Print out each car on a single line
cars.each do |car|
  puts car
end
# Generic car's schematic
class Car
  attr_reader(:brand)
 
  # Initial values for class variables
  # (this happens only once)
  @@brands = []
 
  # Initial value for file
  IO.write('total_cars.txt', 0)
 
  def initialize(brand, sound)
    @brand = brand
    @sound = sound
 
    # Retrieve current value
    current = IO.read('total_cars.txt').to_i
 
    # Write updated value
    IO.write('total_cars.txt', current + 1)
 
    if !@@brands.include?(brand)
      @@brands.push(brand)
    end
  end
 
  def rev
    puts @sound + "! I am a " + @brand
  end
 
  def other_method
    puts 'HI this is another method'
  end
 
  def self.total_cars
    return IO.read('total_cars.txt').to_i
  end
 
  def self.brands
    return @@brands
  end
end
 
 
class RacingCar < Car
  def initialize(brand, sound)
    super(brand, sound)
    @sound = sound.upcase
  end
 
  # def rev
  #   puts @sound.upcase + "! I am a " + @brand
  # end
end
 
 
################################
# Application code starts here #
################################
 
# Build a car
first_car = RacingCar.new('Ferrari', 'Vrooooom')
second_car = RacingCar.new('Mazerati', 'VRRrrrraaa??')
third_car = RacingCar.new('Porsche', 'Put put put')
fourth_car = RacingCar.new('Porsche', 'Pat pat pat')
fifth_car = Car.new('Honda', 'pew pew')
sixth_car = Car.new('Toyota', 'sdfsfsfsfsf')
 
# Use the car
first_car.rev
second_car.rev
third_car.rev
fourth_car.rev
fifth_car.rev
sixth_car.rev
 
puts ''
puts 'TOTAL CARS: ' + Car.total_cars.to_s
 
puts 'REGISTERED BRANDS: ', Car.brands
# Generic car's schematic
class Car
  attr_reader(:brand)
 
  # Initial values for class variables
  # (this happens only once)
  @@brands = []
 
  # Initial value for file
  IO.write('total_cars.txt', 0)
 
  def initialize(brand, sound)
    @brand = brand
    @sound = sound
 
    # Retrieve current value
    current = IO.read('total_cars.txt').to_i
 
    # Write updated value
    IO.write('total_cars.txt', current + 1)
 
    if !@@brands.include?(brand)
      @@brands.push(brand)
    end
  end
 
  def rev
    puts @sound + "! I am a " + @brand
  end
 
  def other_method
    puts 'HI this is another method'
  end
 
  def self.total_cars
    return IO.read('total_cars.txt').to_i
  end
 
  def self.brands
    return @@brands
  end
end
 
 
 
################################
# Application code starts here #
################################
 
# Build a car
first_car = Car.new('Ferrari', 'Vrooooom')
first_car = Car.new('Ferrari', 'Vrooooom')
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Honda', 'pew pew')
second_car = Car.new('Toyota', 'sdfsfsfsfsf')
 
# Use the car
first_car.rev
second_car.rev
 
puts 'TOTAL CARS: ' + Car.total_cars.to_s
 
puts 'REGISTERED BRANDS: ', Car.brands
# Generic car's schematic
class Car
  attr_reader(:brand)
 
  # Initial values for class variables
  # (this happens only once)
  @@number_of_cars = 0
  @@brands = []
 
  def initialize(brand, sound)
    @brand = brand
    @sound = sound
    @@number_of_cars += 1
 
    if !@@brands.include?(brand)
      @@brands.push(brand)
    end
  end
 
  def rev
    puts @sound + "! I am a " + @brand
  end
 
  def other_method
    puts 'HI this is another method'
  end
 
  def self.total_cars
    return @@number_of_cars
  end
 
  def self.brands
    return @@brands
  end
end

#####################################
# Application code start here
#####################################
 
# Build a car
first_car = Car.new('Ferrari', 'Vrooooom')
first_car = Car.new('Ferrari', 'Vrooooom')
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Honda', 'pew pew')
second_car = Car.new('Toyota', 'sdfsfsfsfsf')
 
# Use the car
first_car.rev
second_car.rev
 
puts 'TOTAL CARS: ' + Car.total_cars.to_s
 
puts 'REGISTERED BRANDS: ', Car.brands
class ShoppingCart 

	def initialize           #1  Start with empty array
		@items = []
	end

	def add(item)            #2
		@items.push(item)
	end

	def total_price          #3 
       total = @items.reduce(0) do |current_total, item| 
       	current_total + item.price # add all the prices together and start with zero
       end
	   # each item will have price method that give the final price 
	
       if @items.size >= 5 
    	total = total - total * 0.05 # apply discount
       end

       return total
    end
  end  # end total_price



# Items

class Item
    attr_reader(:name, :price)  # what they have in common

	def initialize(name, price)
       @name = name
       @price = price
   end

end  # end items



  def to_s
    return "#{@name}: #{self.price}"
  end
end



# fruits

class Fruit < Item
	def price 
		now = Time.now
		@price = price

		if now.Saturday? || now.Sunday?  
			price = @price - @price * 0.1     # discount price
		else
			@price  # regular price
		end

		return price
	end

end   # end fruits


# Houseware

class Houseware < Item
	def price
		price = @price
 
        if @price >100  
			price = @price - @price * 0.5
		end

			return price
		end

end  # end Houseware


end # end shopping cart

##################################################
#          Application code starts here          #
##################################################
 
banana = Fruit.new('banana', 10)
vacuum = Houseware.new('vacuum', 150)
blender = Houseware.new('blender', 30)
orange_juice = Item.new('orange juice', 10)
 
cart = ShoppingCart.new
cart.add(banana)
cart.add(banana)
cart.add(banana)
cart.add(banana)
cart.add(banana)
cart.add(orange_juice)
cart.add(vacuum)
 
puts cart.total_price
# Bad
 
class Car
  def initialize(speed)
    @speed = speed
  end
 
  def save
    # Car shouldn't worry about so many details about the saving
    DB.sql("INSERT INTO Cars VALUES #{speed}")
  end
end
 
 
 
 
# Good
 
class Car
  def initialize(speed, storage)
    @speed = speed
    @storage = storage
  end
 
  def save
    # Car doesn't have to worry about how exactly it's being saved
    @storage.really_save(speed)
  end
end
 
 
class CarDatabaseStorage
  def really_save(speed)
    DB.sql("INSERT INTO Cars VALUES #{speed}")
  end
end
 
 
database_storage = CarDatabaseStorage.new
car = Car.new(88, database_storage)
 
 
 
 
#===================================
# In another part of the application
 
car.speed = 100
car.save
# 2 option

class Car 
    attr_reader(:brand, :sound)

    @@number_of_cars = 0
    @@brands = []

	def initialize (brand, sound) 
	    @brand = brand # instances ex
	    @sound = sound
	    @@number_of_cars += 1

	if !@@brands.include?(brand) # no duplicates # is this brand register
	  @@brands.push(brand)
	  end 
	end

	def rev
		puts @sound + "! I'm a " + @brand
	end

	def total_cars
		return @@number_of_cars
	end

	def brands
		return @@brands
    end
    
end

#####################################
# Application code start here
#####################################

first_car = Car.new('Ferrari' , 'Vroomm')  
second_car = Car.new('Audi' , 'Bureung') 
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Porsche', 'Put put put')
second_car = Car.new('Honda', 'pew pew')
second_car = Car.new('Toyota', 'sdfsfsfsfsf')
 
first_car.rev
second_car.rev

puts 'total cars: ' + second_car.total_cars.to_s
puts 'registered brands: ', second_car.brands


# puts 'total cars: ' + Car.total_cars.t_s
# puts 'registered brands: ', Car.brands
ruby --version

#create a file hello.rb
puts "Hello, world!"

cd desktop

#check file on terminal
ruby hello.rb