jimmycababa / rubycodeacademy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

puts "jimmy".length

puts "jimmy".upcase puts "jimmy".downcase

print "What's your first name?'" first_name = gets.chomp first_name.capitalize!

print "What's your last name?" last_name = gets.chomp last_name.capitalize!

print "What city do you live in?" city = gets.chomp city.capitalize!

print "What state are you from?" state = gets.chomp.upcase state.upcase!

puts "Your first name is #{first_name} and your last name is #{last_name}. You are from #{city}, #{state}."

hungry = false

unless hungry puts "I'm writing Ruby programs!" else puts "Time to eat!" end

print "Pleathe enter a thtring: " user_input = gets.chomp user_input.downcase!

if user_input.include? "s" user_input.gsub!(/s/, 'th') else "you did it with no s!" puts "your new phrase is #{user_input}" end

counter = 1 until counter == 11 puts counter counter = counter + 1 end

counter += 1

for num in 1...10

for num in 1..20 puts num end

i = 20 loop do i -= 1 next if i % 2 != 0 print "#{i}" break if i <= 0 end

odds = [1,3,5,7,9]

odds.each do |item| item *= 2 print "#{item}" end

text.split(",)

puts "enter something" text = gets.chomp

puts "enter words to be redacted" redact = gets.chomp

words = text.split(" ")

words.each { |letter| if letter == redact print "REDACTED " else print letter + " "

end}

my_2d_array = [["hi"],["there"],["fartface]]

pets = Hash.new pets["wynter"] = 'dog'

puts pets["wynter"]

languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"] languages.each{ |butt| puts butt}

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]] s.each do |sub_array| sub_array.each do |x| puts x end end

secret_identities = { "The Batman" => "Bruce Wayne", "Superman" => "Clark Kent", "Wonder Woman" => "Diana Prince", "Freakazoid" => "Dexter Douglas" } secret_identities.each do |hero, normal| puts "#{hero}: #{normal}" end

puts "Enter a phrase you'd like to analyze: " text = gets.chomp

words = text.split

frequencies = Hash.new(0)

words.each { |word| frequencies[word] += 1 }

frequencies = frequencies.sort_by do |word, count| count end

frequencies.reverse!

frequencies.each do |word, count| puts word + " " + count.to_s end

def what_up(greeting, *friends)

my_array = [1, 2, 3, 4, 5] my_array.each do |num| puts num * num end

fruits = ["orange", "apple", "banana", "pear", "grapes"] fruits.sort! do |firstFruit, secondFruit| secondFruit <=> firstFruit end

def alphabetize(arr, rev = false) arr.sort!

if rev == true arr.reverse! else arr end end

numbers = [1, 3, 5] puts alphabetize(numbers)

symbols = [] strings.each do |s| if s == strings symbols.push(s.to_sym) end end print symbols

movie_ratings = { memento: 3, primer: 3.5, the_matrix: 3, truman_show: 4, red_dawn: 1.5, skyfall: 4, alex_cross: 2, uhf: 1, lion_king: 3.5 } puts movie_ratings.select { |movie, rating| puts movie}

puts "What's your favorite language?" language = gets.chomp

case language when "Ruby" puts "Ruby is great for web apps!" when "Python" puts "Python is great for science." when "JavaScript" puts "JavaScript makes websites awesome." when "HTML" puts "HTML is what websites are made of!" when "CSS" puts "CSS makes websites pretty." else puts "I don't know that language!" end

movies = { Memento: 3, Primer: 4, Ishtar: 1 }

puts "What would you like to do?" puts "-- Type 'add' to add a movie." puts "-- Type 'update' to update a movie." puts "-- Type 'display' to display all movies." puts "-- Type 'delete' to delete a movie."

choice = gets.chomp.downcase case choice when 'add' puts "What movie do you want to add?" title = gets.chomp if movies[title.to_sym].nil? puts "What's the rating? (Type a number 0 to 4.)" rating = gets.chomp movies[title.to_sym] = rating.to_i puts "#{title} has been added with a rating of #{rating}." else puts "That movie already exists! Its rating is #{movies[title.to_sym]}." end when 'update' puts "What movie do you want to update?" title = gets.chomp if movies[title.to_sym].nil? puts "Movie not found!" else puts "What's the new rating? (Type a number 0 to 4.)" rating = gets.chomp movies[title.to_sym] = rating.to_i puts "#{title} has been updated with new rating of #{rating}." end when 'display' movies.each do |movie, rating| puts "#{movie}: #{rating}" end when 'delete' puts "What movie do you want to delete?" title = gets.chomp if movies[title.to_sym].nil? puts "Movie not found!" else movies.delete(title.to_sym) puts "#{title} has been removed." end else puts "Sorry, I didn't understand you." end

puts "I love love" if true

puts 1 < 2? "One is less than two!" : "One is not less than two."

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

my_array.each do |x| puts x % 2 == 0? x : nil end

"L".upto("P") do |letter| puts letter end

favorite_animal ||= "tiger"

5.times do |x| puts "I'm a block!" end

def yield_name(name) puts "In the method! Let's yield." yield("Kim") puts "In between the yields!" yield(name) puts "Block complete! Back in the method." end

yield_name("Eric") { |n| puts "My name is #{n}." }

yield_name("jimmy") { |n| puts "My name is #{n}!"}

round_down = Proc.new do |x| x.floor

hi = Proc.new { puts "Hello!"} hi.call

strings = ["leonardo", "donatello", "raphael", "michaelangelo"]

Write your code below this line!

symbolize = lambda { |x| x.to_sym}

Write your code above this line!

symbols = strings.collect(&symbolize) print symbols

symbolize = lambda { |x| x.to_sym}

my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]

Add your code below!

symbol_filter = lambda { |x| x.is_a? Symbol } symbols = my_array.select(&symbol_filter) puts symbols

class Language def initialize(name, creator) @name = name @creator = creator end

def description puts "I'm #{@name} and I was created by #{@creator}!" end end

ruby = Language.new("Ruby", "Yukihiro Matsumoto") python = Language.new("Python", "Guido van Rossum") javascript = Language.new("JavaScript", "Brendan Eich")

ruby.description python.description javascript.description

class ApplicationError def display_error puts "Error! Error!" end end

class SuperBadError < ApplicationError end

err = SuperBadError.new err.display_error

class Creature def initialize(name) @name = name end

def fight return "Punch to the chops!" end end

Add your code below!

class Dragon < Creature def fight puts "Instead of breathing fire..." super end end

"Instead of breathing fire..." "Punch to the chops!"

class Message @@messages_sent = 0 def initialize(from, to) @from = from @to = to @@messages_sent += 1 end end

my_message = Message.new("butts", "faces")

class Email < Message def initialize(from, to) super end end

class Computer @@users = {} def initialize(username, password) @username = username @password = password @files = {} @@users[username] = password end

def create(filename) time = Time.now @files[filename] = time puts "#{filename} was created at #{time} by #{@username}. " end

def Computer.get_users @@users

end end my_computer = Computer.new("my shit", "othershit")

About