daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 1. ARRAYS
  2. arr1 = ['a', 'b', 'c']
  3. arr1 << 'd'
  4. arr2 = (0..4).to_a
  5. arr3 = ['blue', 'green', 'red']
  6. arr4 = [4, 5, 3]
  7. h1 = arr3.zip(arr4).to_h
  8. h2 = [arr3, arr4].transpose.to_h
  9.  
  10. # 2. HASHES
  11. h1 = {street: '123 Main St', city: 'Austin', state: 'TX'}
  12. h1.slice(:street) #=> {:street=>"123 Main St"}
  13. h1[:country] = 'USA'
  14. h1.keys
  15. h1.values
  16.  
  17. # 3. ITERATION - EACH
  18. h1.each do |k,v|
  19.   puts "#{k} is key, and #{v} is value."
  20. end
  21.  
  22. (1..5).each do |i|
  23.   puts "iteration #{i}"
  24. end
  25.  
  26. # 4. ITERATION - WHILE
  27. i = 1
  28. while i <= 5
  29.   puts "Number #{i}"
  30.   i+=1
  31. end
  32.  
  33. # 5. ITERATION - FOR
  34. for i in 6..10
  35.    puts "Number #{i}"
  36. end
  37.  
  38. # 6. ITERATION - UNTIL
  39. i = 1
  40. limit = 5
  41.  
  42. until i > limit  do
  43.   puts "Number #{i}"
  44.   i += 1
  45. end
  46.  
  47. # 7. CONDITIONAL
  48. season = 'fall'
  49.  
  50. if season == 'spring'
  51.   puts "It's getting warm."
  52. elsif season == 'summer'
  53.   puts "It's so hot!"
  54. elsif season == 'fall'
  55.   puts "It's really windy!"
  56. elsif season == 'winter'
  57.   puts "It's freezing!"
  58. else
  59.   puts "Please enter a valid season"
  60. end
  61.  
  62. # 8. CASE STATEMENTS
  63. grade = 'B'
  64.  
  65. case grade
  66. when 'A'
  67.   puts "Way to go kiddo"
  68. when 'B'
  69.   puts "Better luck next time"
  70. when 'C'
  71.   puts "You can do better"
  72. when 'D'
  73.   puts "Scraping through"
  74. when 'F'
  75.   puts "You failed!"
  76. else
  77.   puts "Alternative grading system, eh?"
  78. end
  79.  
  80. # 9. Methods
  81. def greet(name)
  82.   puts "Welcome, #{name}!"
  83. end
  84. greet('Adam')
  85.  
  86. def sum(x,y)
  87.   x + y
  88. end
  89. sum(3, 4)
  90.  
  91. def guest_list(*gusts)
  92.    gusts.each { |guest| puts "#{guest}" }
  93. end
  94. guest_list('Tom', 'Mary')
  95.  
  96. # 10. Classes
  97. class Bank
  98.   # attr_accessor :deposit
  99.  
  100.   def initialize
  101.     # enter_password
  102.     # check_password
  103.     @password = nil
  104.     @balance = nil
  105.     @name = nil
  106.   end
  107.  
  108.   def enter_password
  109.     puts "Please enter your password."
  110.     @password = gets.chomp
  111.     @balance = 100
  112.     @name = 'Tom'
  113.   end
  114.  
  115.   def check_password
  116.     if @password == "1234"
  117.       @balance = 100
  118.       @name = 'Tom'
  119.       greeting
  120.     else
  121.       puts "Sorry, password not recognized"
  122.       enter_password
  123.     end
  124.   end
  125.  
  126.   def greeting
  127.     puts "Welcome, #{@name}.  How can we help you today?"
  128.   end
  129.  
  130.   def deposit(amount)
  131.     update = @balance + amount
  132.     puts "Deposit of $#{amount} + $#{@balance} balance = $#{update} new total."
  133.     @balance = update
  134.   end
  135.  
  136.   def withdrawel(amount)
  137.     update = @balance - amount
  138.     puts "Withdrawel of $#{amount} from $#{@balance} balance = $#{update} new total."
  139.     @balance = update
  140.   end
  141.  
  142. end
  143.  
  144. bank = Bank.new
  145. bank.deposit(20)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top