Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " #{smth" \n \t" in double quotes
- "tatiana".length
- => 7
- s.empty?
- s.include?("foo")
- && || !
- c.to_s (str)
- ==============REVERSE================
- "abc".reverse #cba
- ============EMPTY/if/else============
- def string_message(str = '')
- >> if str.empty?
- >> "It's an empty string!"
- >> else
- >> "The string is nonempty."
- >> end
- >> end
- => :string_message
- >> puts string_message("foobar")
- The string is nonempty.
- ===========RETURN=============
- >> def string_message(str = '')
- >> return "It's an empty string!" if str.empty?
- >> return "The string is nonempty."
- >> end
- ==================ARRAY=======================
- "foo bar baz".split # Split a string into a three-element array.
- => ["foo", "bar", "baz"]
- "fooxbarxbaz".split('x')
- => ["foo", "bar", "baz"]
- >> a = [42, 8, 17]
- => [42, 8, 17]
- >> a[0] # Ruby uses square brackets for array access.
- => 42
- >> a[1]
- => 8
- >> a[2]
- => 17
- >> a[-1] # Indices can even be negative!
- => 17
- >> a # Just a reminder of what 'a' is
- => [42, 8, 17]
- >> a.first
- => 42
- >> a.second
- => 8
- >> a.last
- => 17
- >> a.last == a[-1] # Comparison using ==
- => true
- >> a
- => [42, 8, 17]
- >> a.empty?
- => false
- >> a.include?(42)
- => true
- >> a.sort
- => [8, 17, 42]
- >> a.reverse
- => [17, 8, 42]
- >> a.shuffle
- => [17, 42, 8]
- >> a
- => [42, 8, 17]
- >> a.sort! sort! - chanche the arr
- => [8, 17, 42]
- >> a
- => [8, 17, 42]
- a.push(6) # Pushing 6 onto an array
- => [42, 8, 17, 6]
- >> a << 7 # Pushing 7 onto an array
- => [42, 8, 17, 6, 7]
- >> a << "foo" << "bar" # Chaining array pushes
- => [42, 8, 17, 6, 7, "foo", "bar"]
- >> a.join # Join on nothing.
- => "4281767foobar"
- >> a.join(', ') # Join on comma-space.
- => "42, 8, 17, 6, 7, foo, bar"
- >> (0..9).to_a # Use parentheses to call to_a on the range.
- => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- =============Ranges========================
- >> a = %w[foo bar baz quux] # Use %w to make a string array.
- => ["foo", "bar", "baz", "quux"]
- >> a[0..2]
- => ["foo", "bar", "baz"]
- a[2..(a.length-1)] # Explicitly use the array's length.
- => [2, 3, 4, 5, 6, 7, 8, 9]
- ==================================
- 3.times { puts "Betelgeuse!" } # 3.times takes a block with no variables.
- "Betelgeuse!"
- "Betelgeuse!"
- "Betelgeuse!"
- => 3
- >> (1..5).map { |i| i**2 } # The ** notation is for 'power'.
- => [1, 4, 9, 16, 25]
- >> %w[a b c] # Recall that %w makes string arrays.
- => ["a", "b", "c"]
- >> %w[a b c].map { |char| char.upcase }
- => ["A", "B", "C"]
- >> %w[A B C].map { |char| char.downcase }
- => ["a", "b", "c"]
- -------------or----------------
- %w[A B C].map(&:downcase)
- => ["a", "b", "c"]
- ('a'..'z').to_a.shuffle[0..7].join
- ==============================Hash============================
- user = { "first_name" => "Michael", "last_name" => "Hartl" }
- => {"last_name"=>"Hartl", "first_name"=>"Michael"}
- >> "name".split('')
- => ["n", "a", "m", "e"]
- >> :name.split('')
- NoMethodError: undefined method `split' for :name:Symbol
- >> "foobar".reverse
- => "raboof"
- >> :foobar.reverse
- NoMethodError: undefined method `reverse' for :foobar:Symbol
- >> user = { :name => "Michael Hartl", :email => "michael@example.com" }
- => {:name=>"Michael Hartl", :email=>"michael@example.com"}
- >> h1 = { :name => "Michael Hartl", :email => "michael@example.com" }
- => {:name=>"Michael Hartl", :email=>"michael@example.com"}
- >> h2 = { name: "Michael Hartl", email: "michael@example.com" }
- => {:name=>"Michael Hartl", :email=>"michael@example.com"}
- >> h1 == h2
- => true
- >> params[:user] = { name: "Michael Hartl", email: "mhartl@example.com" }
- => {:name=>"Michael Hartl", :email=>"mhartl@example.com"}
- >> params
- => {:user=>{:name=>"Michael Hartl", :email=>"mhartl@example.com"}}
- >> params[:user][:email]
- => "mhartl@example.com"
- >> params = {} # Define a hash called 'params' (short for 'parameters'). !!!!!
- => {}
- >> params[:user] = { name: "Michael Hartl", email: "mhartl@example.com" }
- => {:name=>"Michael Hartl", :email=>"mhartl@example.com"}
- >> params
- => {:user=>{:name=>"Michael Hartl", :email=>"mhartl@example.com"}}
- >> params[:user][:email]
- => "mhartl@example.com"
- -----------------------------------------------------------
- person1 = {:first => 'Al', :last => 'Bundy'}
- person2 = {:first => 'Peggy', :last => 'Bundy'}
- person3 = {:first => 'Kelly', :last => 'Bundy'}
- params = {
- :father => person1,
- :mother => person2,
- :child => person3
- }
- params[:father][:first] #=> 'Al'
- =============CSS==================
- stylesheet_link_tag 'application', { media: 'all',
- 'data-turbolinks-track': 'reload' }
- stylesheet_link_tag 'application', media: 'all',
- 'data-turbolinks-track': 'reload'
- =================================Validation===============================================
- >> %w[foo bar baz]
- => ["foo", "bar", "baz"]
- >> addresses = %w[USER@foo.COM THE_US-ER@foo.bar.org first.last@foo.jp]
- => ["USER@foo.COM", "THE_US-ER@foo.bar.org", "first.last@foo.jp"]
- >> addresses.each do |address|
- ?> puts address
- >> end
- USER@foo.COM
- THE_US-ER@foo.bar.org
- first.last@foo.jp
Add Comment
Please, Sign In to add comment