Guest User

Untitled

a guest
Mar 4th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. " #{smth" \n \t" in double quotes
  2.  
  3. "tatiana".length
  4. => 7
  5.  
  6. s.empty?
  7.  
  8. s.include?("foo")
  9.  
  10. && || !
  11.  
  12. c.to_s (str)
  13. ==============REVERSE================
  14. "abc".reverse #cba
  15. ============EMPTY/if/else============
  16. def string_message(str = '')
  17. >> if str.empty?
  18. >> "It's an empty string!"
  19. >> else
  20. >> "The string is nonempty."
  21. >> end
  22. >> end
  23. => :string_message
  24. >> puts string_message("foobar")
  25. The string is nonempty.
  26. ===========RETURN=============
  27. >> def string_message(str = '')
  28. >> return "It's an empty string!" if str.empty?
  29. >> return "The string is nonempty."
  30. >> end
  31.  
  32. ==================ARRAY=======================
  33. "foo bar baz".split # Split a string into a three-element array.
  34. => ["foo", "bar", "baz"]
  35.  
  36. "fooxbarxbaz".split('x')
  37. => ["foo", "bar", "baz"]
  38.  
  39. >> a = [42, 8, 17]
  40. => [42, 8, 17]
  41. >> a[0] # Ruby uses square brackets for array access.
  42. => 42
  43. >> a[1]
  44. => 8
  45. >> a[2]
  46. => 17
  47. >> a[-1] # Indices can even be negative!
  48. => 17
  49.  
  50. >> a # Just a reminder of what 'a' is
  51. => [42, 8, 17]
  52. >> a.first
  53. => 42
  54. >> a.second
  55. => 8
  56. >> a.last
  57. => 17
  58. >> a.last == a[-1] # Comparison using ==
  59. => true
  60.  
  61. >> a
  62. => [42, 8, 17]
  63. >> a.empty?
  64. => false
  65. >> a.include?(42)
  66. => true
  67. >> a.sort
  68. => [8, 17, 42]
  69. >> a.reverse
  70. => [17, 8, 42]
  71. >> a.shuffle
  72. => [17, 42, 8]
  73. >> a
  74. => [42, 8, 17]
  75.  
  76. >> a.sort! sort! - chanche the arr
  77. => [8, 17, 42]
  78. >> a
  79. => [8, 17, 42]
  80.  
  81. a.push(6) # Pushing 6 onto an array
  82. => [42, 8, 17, 6]
  83. >> a << 7 # Pushing 7 onto an array
  84. => [42, 8, 17, 6, 7]
  85. >> a << "foo" << "bar" # Chaining array pushes
  86. => [42, 8, 17, 6, 7, "foo", "bar"]
  87.  
  88. >> a.join # Join on nothing.
  89. => "4281767foobar"
  90. >> a.join(', ') # Join on comma-space.
  91. => "42, 8, 17, 6, 7, foo, bar"
  92.  
  93. >> (0..9).to_a # Use parentheses to call to_a on the range.
  94. => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  95.  
  96. =============Ranges========================
  97. >> a = %w[foo bar baz quux] # Use %w to make a string array.
  98. => ["foo", "bar", "baz", "quux"]
  99. >> a[0..2]
  100. => ["foo", "bar", "baz"]
  101.  
  102. a[2..(a.length-1)] # Explicitly use the array's length.
  103. => [2, 3, 4, 5, 6, 7, 8, 9]
  104.  
  105. ==================================
  106. 3.times { puts "Betelgeuse!" } # 3.times takes a block with no variables.
  107. "Betelgeuse!"
  108. "Betelgeuse!"
  109. "Betelgeuse!"
  110. => 3
  111. >> (1..5).map { |i| i**2 } # The ** notation is for 'power'.
  112. => [1, 4, 9, 16, 25]
  113. >> %w[a b c] # Recall that %w makes string arrays.
  114. => ["a", "b", "c"]
  115. >> %w[a b c].map { |char| char.upcase }
  116. => ["A", "B", "C"]
  117. >> %w[A B C].map { |char| char.downcase }
  118. => ["a", "b", "c"]
  119. -------------or----------------
  120. %w[A B C].map(&:downcase)
  121. => ["a", "b", "c"]
  122.  
  123. ('a'..'z').to_a.shuffle[0..7].join
  124. ==============================Hash============================
  125. user = { "first_name" => "Michael", "last_name" => "Hartl" }
  126. => {"last_name"=>"Hartl", "first_name"=>"Michael"}
  127.  
  128. >> "name".split('')
  129. => ["n", "a", "m", "e"]
  130. >> :name.split('')
  131. NoMethodError: undefined method `split' for :name:Symbol
  132. >> "foobar".reverse
  133. => "raboof"
  134. >> :foobar.reverse
  135. NoMethodError: undefined method `reverse' for :foobar:Symbol
  136.  
  137. >> user = { :name => "Michael Hartl", :email => "michael@example.com" }
  138. => {:name=>"Michael Hartl", :email=>"michael@example.com"}
  139.  
  140. >> h1 = { :name => "Michael Hartl", :email => "michael@example.com" }
  141. => {:name=>"Michael Hartl", :email=>"michael@example.com"}
  142. >> h2 = { name: "Michael Hartl", email: "michael@example.com" }
  143. => {:name=>"Michael Hartl", :email=>"michael@example.com"}
  144. >> h1 == h2
  145. => true
  146.  
  147. >> params[:user] = { name: "Michael Hartl", email: "mhartl@example.com" }
  148. => {:name=>"Michael Hartl", :email=>"mhartl@example.com"}
  149. >> params
  150. => {:user=>{:name=>"Michael Hartl", :email=>"mhartl@example.com"}}
  151. >> params[:user][:email]
  152. => "mhartl@example.com"
  153.  
  154. >> params = {} # Define a hash called 'params' (short for 'parameters'). !!!!!
  155. => {}
  156. >> params[:user] = { name: "Michael Hartl", email: "mhartl@example.com" }
  157. => {:name=>"Michael Hartl", :email=>"mhartl@example.com"}
  158. >> params
  159. => {:user=>{:name=>"Michael Hartl", :email=>"mhartl@example.com"}}
  160. >> params[:user][:email]
  161. => "mhartl@example.com"
  162. -----------------------------------------------------------
  163. person1 = {:first => 'Al', :last => 'Bundy'}
  164. person2 = {:first => 'Peggy', :last => 'Bundy'}
  165. person3 = {:first => 'Kelly', :last => 'Bundy'}
  166. params = {
  167. :father => person1,
  168. :mother => person2,
  169. :child => person3
  170. }
  171. params[:father][:first] #=> 'Al'
  172. =============CSS==================
  173. stylesheet_link_tag 'application', { media: 'all',
  174. 'data-turbolinks-track': 'reload' }
  175. stylesheet_link_tag 'application', media: 'all',
  176. 'data-turbolinks-track': 'reload'
  177.  
  178.  
  179. =================================Validation===============================================
  180. >> %w[foo bar baz]
  181. => ["foo", "bar", "baz"]
  182. >> addresses = %w[USER@foo.COM THE_US-ER@foo.bar.org first.last@foo.jp]
  183. => ["USER@foo.COM", "THE_US-ER@foo.bar.org", "first.last@foo.jp"]
  184. >> addresses.each do |address|
  185. ?> puts address
  186. >> end
  187. USER@foo.COM
  188. THE_US-ER@foo.bar.org
  189. first.last@foo.jp
Add Comment
Please, Sign In to add comment