<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aizat Ibraimova</title>
    <description>The latest articles on DEV Community by Aizat Ibraimova (@aizatibraimova).</description>
    <link>https://dev.to/aizatibraimova</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1250640%2F1866675a-ac35-4122-99c7-83d9b1ae0bd3.jpeg</url>
      <title>DEV Community: Aizat Ibraimova</title>
      <link>https://dev.to/aizatibraimova</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aizatibraimova"/>
    <language>en</language>
    <item>
      <title>VS Code shortcuts</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Mon, 03 Jun 2024 15:11:46 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/vs-code-shortcuts-257f</link>
      <guid>https://dev.to/aizatibraimova/vs-code-shortcuts-257f</guid>
      <description>&lt;p&gt;&lt;strong&gt;%&lt;/strong&gt;  bash prompt&lt;br&gt;
&lt;strong&gt;Ctrl + C&lt;/strong&gt; cancel any currently running terminal&lt;br&gt;
&lt;strong&gt;pry or rails console&lt;/strong&gt; for interacting with a database&lt;br&gt;
&lt;strong&gt;q&lt;/strong&gt; to quit&lt;br&gt;
&lt;strong&gt;cmd + K&lt;/strong&gt; clear terminal&lt;br&gt;
&lt;strong&gt;cmd + Shift + P&lt;/strong&gt; Command Palette(fuzzy search)&lt;br&gt;
&lt;strong&gt;cmd + P&lt;/strong&gt; quick open file&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option + Cmd + J&lt;/strong&gt; hard refresh in Chrome&lt;br&gt;
&lt;strong&gt;ctrl + A/Cmd + A&lt;/strong&gt; Jump back to the beginning of the line&lt;br&gt;
&lt;strong&gt;Ctrl + E&lt;/strong&gt; ed of the line&lt;br&gt;
&lt;strong&gt;Cmd + F&lt;/strong&gt; find(and replace)&lt;br&gt;
&lt;strong&gt;Cmd + Shift + F&lt;/strong&gt; find (and replace) Global&lt;br&gt;
&lt;strong&gt;Cmd + D&lt;/strong&gt; Find next selection&lt;br&gt;
&lt;strong&gt;Shift + Option + Down&lt;/strong&gt; Duplicate lines&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hashes and Symbols</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Wed, 24 Jan 2024 17:28:34 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/hashes-and-symbols-p4m</link>
      <guid>https://dev.to/aizatibraimova/hashes-and-symbols-p4m</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby Symbols&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, &lt;em&gt;symbols&lt;/em&gt; are immutable names primarily used as hash keys or for referencing method names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_bologna = {
  :first_name =&amp;gt; "Oscar",
  :second_name =&amp;gt; "Meyer",
  :slices =&amp;gt; 12
}

puts my_bologna[:second_name] # =&amp;gt; Meyer

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;em&gt;Symbol Syntax&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Symbols always start with a colon (&lt;code&gt;:&lt;/code&gt;). They must be valid Ruby variable names, so the first character after the colon has to be a letter or underscore (&lt;code&gt;_&lt;/code&gt;); after that, any combination of letters, numbers, and underscores is allowed.&lt;/p&gt;

&lt;p&gt;Make sure you don’t put any spaces in your symbol name—if you do, Ruby will get confused.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:my symbol # Don't do this!
:my_symbol # Do this instead.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;em&gt;What are Symbols Used For?&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Symbols pop up in a lot of places in Ruby, but they’re primarily used either as hash keys or for referencing method names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sounds = {
  :cat =&amp;gt; "meow",
  :dog =&amp;gt; "woof",
  :computer =&amp;gt; 10010110,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Symbols make good hash keys for a few reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They’re immutable, meaning they can’t be changed once they’re created;&lt;/li&gt;
&lt;li&gt;Only one copy of any symbol exists at a given time, so they save memory;&lt;/li&gt;
&lt;li&gt;Symbol-as-keys are faster than strings-as-keys because of the above two reasons.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Ruby Hashes, Symbols, &amp;amp; Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby hashes, key symbols and their values can be defined in either of two ways, using a &lt;code&gt;=&amp;gt;&lt;/code&gt; or &lt;code&gt;:&lt;/code&gt; to separate symbol keys from values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_progress = {
  :program =&amp;gt; "Codecademy",
  :language =&amp;gt; "Ruby",
  :enthusiastic? =&amp;gt; true 
}
#Key symbols and their values can be defined with a =&amp;gt;, also known as a hash rocket.

my_progress = {
  program: "Codecademy",
  language: "Ruby",
  enthusiastic?: true 
}
#Key symbols and their values can also be defined with the colon (:) at the end of the symbol followed by its value.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Converting Between Symbols and Strings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Converting between strings and symbols is a snap.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:sasquatch.to_s
# ==&amp;gt; "sasquatch"

"sasquatch".to_sym
# ==&amp;gt; :sasquatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.to_s&lt;/code&gt; and &lt;code&gt;.to_sym&lt;/code&gt; methods are what you’re looking for!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

symbols = []

strings.each do |s|
 symbols.push(s.to_sym)
end
print symbols
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Besides using &lt;code&gt;.to_sym&lt;/code&gt;, you can also use &lt;code&gt;.intern&lt;/code&gt;. This will internalize the string into a symbol and works just like &lt;code&gt;.to_sym&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello".intern
# ==&amp;gt; :hello

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby’s &lt;code&gt;.to_sym&lt;/code&gt; method can convert a string to a symbol, and &lt;code&gt;.to_i&lt;/code&gt; will convert a string to an integer.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Ruby .select Method&lt;/strong&gt;&lt;br&gt;
In Ruby, the &lt;code&gt;.select&lt;/code&gt; method can be used to grab specific values from a hash that meet a certain criteria.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;olympic_trials = {
  Sally: 9.58,
  John: 9.69,
  Bob: 14.91
}

olympic_trials.select { |name, time| time &amp;lt;  10.05 }
#The example above returns {:Sally=&amp;gt;9.58, :John=&amp;gt;9.69} since Sally and John are the only keys whose values meet the time &amp;lt; 10.05 criteria.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby .each_key &amp;amp; .each_value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;code&gt;.each_key&lt;/code&gt; and &lt;code&gt;.each_value&lt;/code&gt; methods are used to iterate over only the keys or only the values in a hash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_hash = { one: 1, two: 2, three: 3 }

my_hash.each_key { |k| print k, " " }
# ==&amp;gt; one two three

my_hash.each_value { |v| print v, " " }
# ==&amp;gt; 1 2 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;The Case Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; are powerful, but we can get bogged down in i&lt;code&gt;f&lt;/code&gt;s and &lt;code&gt;elsif&lt;/code&gt;s if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the &lt;code&gt;case&lt;/code&gt; statement. The syntax looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;case language
  when "JS"
    puts "Websites!"
  when "Python"
    puts "Science!"
  when "Ruby"
    puts "Web apps!"
  else
    puts "I don't know!"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;A NIGHT AT THE MOVIES&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;movies = {
  harry_potter: 4
}
puts "What would you like to do?"

choice = gets.chomp

case choice
 when "add"
   puts "What movie would you like to add?"
   title = gets.chomp
   if movies[title.to_sym].nil?
   puts "What rating does the movie have? (Type a number from 0 to 4)"
   rating = gets.chomp
   movies[title.to_sym] = rating.to_i
   puts "#{title} was 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 would you like to update?'
   title = gets.chomp
   if movies[title].nil?
   puts "There has been an error! Please check the spelling again."
   else
   puts "What is the new rating?"
   rating = gets.chomp
   movies[title.intern] = rating.to_i
   puts "#{title} has been updated with new rating of #{rating}."
   end
 when "display"
   movies.each { |movie, rating| 
   puts "#{movie}: #{rating}"}
 when "delete"
   puts "What is the title of the movie you would like to delete?"
   title = gets.chomp
   if movies[title.intern].nil?
   puts "The movie was not found"
   else
   movies.delete(title.to_sym)
   puts "#{title} has been removed."
   end
 else 
 puts "Error!"
end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Blocks and Sorting</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Wed, 24 Jan 2024 04:39:03 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/blocks-and-sorting-3do0</link>
      <guid>https://dev.to/aizatibraimova/blocks-and-sorting-3do0</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby method&lt;/strong&gt;&lt;br&gt;
(like JS functions)&lt;/p&gt;

&lt;p&gt;A Ruby &lt;em&gt;method&lt;/em&gt; is a reusable section of code written to execute a certain task. It is defined with the &lt;code&gt;def&lt;/code&gt; keyword, followed by a method name, a method body, and ends with the &lt;code&gt;end&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greeting
  puts "Hello world!"
end

#In this example, the first line or header contains the keyword "def" and the method name. puts "Hello world!" is within the body of the method, which describes the certain task that the method carries out. It is also indented two spaces by convention. Following the body, the method ends with the end keyword.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Method Parameters &amp;amp; Arguments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, &lt;em&gt;parameters&lt;/em&gt; are placeholders for real values or &lt;em&gt;arguments&lt;/em&gt; passed into a method when it is called. When calling a method that requires parameters, arguments (ie. real values) must be passed in for those parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def square(num) # num is the parameter
  puts num ** 2
end

square(5) #5 is the argument
#Output =&amp;gt; 25

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Method Splat&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a Ruby method, a splat (&lt;code&gt;*&lt;/code&gt;) operator is used to indicate that a parameter can have an unknown number of arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#The * preceding the parameter "clubs" allows for multiple arguments to be passed into the method when you actually call it.
def extra_curriculars(*clubs)
  clubs.each { |club| puts "After school, I'm involved with #{club}" }
end

extra_curriculars("chess club", "gymnastics", "anime club", "library services")

#Output
#After school, I'm involved with chess club
#After school, I'm involved with gymnastics
#After school, I'm involved with anime club
#After school, I'm involved with library services
Ruby Block Parameter

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Return&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;code&gt;return&lt;/code&gt; keyword is used to pass back a value from a method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def generous_tip(bill)
  return bill * (0.25)
end

generous_tip(100) # 25

#In this example, the generous_tip method is returning the product of bill and 0.25. In order to see that value, a "puts" or "print" can be added before the method call.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Block&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(like anonymous JS functions)&lt;/p&gt;

&lt;p&gt;In Ruby, a &lt;em&gt;block&lt;/em&gt; is a section of code defined within the keywords &lt;code&gt;do&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; or with curly braces &lt;code&gt;{}&lt;/code&gt;. This is usually preceded by an integer followed by &lt;code&gt;.times&lt;/code&gt; to indicate how many times the code is to be executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2.times do
  puts "I'm a code block!"
end  

#Output
#I'm a code block!
#I'm a code block!

3.times { puts "So am I!" }

#Output
#"So am I!"
#"So am I!"
#"So am I!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;How Blocks Differ from Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are some differences between blocks and methods, however.&lt;/p&gt;

&lt;p&gt;Check out the code in the editor. The &lt;code&gt;capitalize&lt;/code&gt; method capitalizes a word, and we can continually invoke the &lt;code&gt;capitalize&lt;/code&gt; method by name. We can &lt;code&gt;capitalize("matz")&lt;/code&gt;, &lt;code&gt;capitalize("Eduardo")&lt;/code&gt;, or any string we like to our hearts’ content.&lt;/p&gt;

&lt;p&gt;However, the block that we define (following &lt;code&gt;.each)&lt;/code&gt; will only be called &lt;em&gt;once&lt;/em&gt;, and in the context of the array that we are iterating over. It appears just long enough to do some work for &lt;code&gt;each&lt;/code&gt;, then vanishes into the night.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# method that capitalizes a word
def capitalize(string) 
  puts "#{string[0].upcase}#{string[1..-1]}"
end

capitalize("ryan") # prints "Ryan"
capitalize("jane") # prints "Jane"

# block that capitalizes each string in the array
["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Block Parameter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, a method can take a &lt;em&gt;block&lt;/em&gt; as a parameter.&lt;/p&gt;

&lt;p&gt;Passing a &lt;em&gt;block&lt;/em&gt; to a method is a great way of abstracting certain tasks from the method and defining those tasks when we call the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# The block, {|i| puts i}, is passed the current array item each time it is evaluated. This block prints the item. 
[1, 2, 3, 4, 5].each { |i| puts i }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Sort Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;code&gt;.sort&lt;/code&gt; array method is used to sort items in an array in ascending order (least to greatest).&lt;/p&gt;

&lt;p&gt;In Ruby, there are two sorting methods, &lt;code&gt;.sort&lt;/code&gt; or &lt;code&gt;sort!&lt;/code&gt;. The first method, &lt;code&gt;.sort&lt;/code&gt;, simply returns a sorted array while leaving the original array alone. The second method, &lt;code&gt;.sort!&lt;/code&gt;, modifies the actual array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_array = [3, 4, 8, 7, 1, 6, 5, 9, 2]
my_array.sort!
#Attaching an ! to the end of .sort or any other Ruby method modifies the original array.
print my_array
# =&amp;gt; [1, 2, 3, 4, 5, 6, 7, 8, 9]
#If you didn't use !, print my_array returns the original array.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Combined Comparison Operator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;em&gt;combined comparison operator,&lt;/em&gt; &lt;code&gt;&amp;lt;=&amp;gt;&lt;/code&gt;, also known as the spaceship operator is used to compare two objects. It returns &lt;code&gt;0&lt;/code&gt; if the first operand equals the second, &lt;code&gt;1&lt;/code&gt; if the first operand is greater than the second, and &lt;code&gt;-1&lt;/code&gt; if the first operand is less than the second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "Keanu" &amp;lt;=&amp;gt; "Adrianna" # The first letters of each word are compared in ASCII order and since "K" comes after "A", 1 is printed.

puts 1 &amp;lt;=&amp;gt; 2 # -1

puts 3 &amp;lt;=&amp;gt; 3 # 0

#&amp;lt;=&amp;gt; can also be used inside of a block and to sort values in descending order:
my_array = [3, 0, 8, 7, 1, 6, 5, 9, 4]
my_array.sort! { |first_num, second_num| second_num &amp;lt;=&amp;gt; first_num }
print my_array
#Output =&amp;gt; [9, 8, 7, 6, 5, 4, 3, 1, 0]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;ORDERING YOUR LIBRARY&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def alphabetize(arr, rev=false)
  if rev
    arr.sort { |item1, item2| item2 &amp;lt;=&amp;gt; item1 }
  else
    arr.sort { |item1, item2| item1 &amp;lt;=&amp;gt; item2 }
  end
end

books = ["Heart of Darkness", "Code Complete", "The Lorax", "The Prophet", "Absalom, Absalom!"]

puts "A-Z: #{alphabetize(books)}"
puts "Z-A: #{alphabetize(books, true)}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def alphabetize(arr, rev = false)
  arr.sort!
  if rev == true
  return arr.reverse!
  else
  return arr
  end
end

numbers = [3, 5, 1, 6]

puts alphabetize(numbers)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Arrays and Hashes</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Wed, 24 Jan 2024 02:45:34 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/arrays-and-hashes-4b12</link>
      <guid>https://dev.to/aizatibraimova/arrays-and-hashes-4b12</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, an array is an ordered collection of Ruby objects separated by commas and enclosed in &lt;code&gt;[]&lt;/code&gt;. An array can contain the same or different types of Ruby objects, such as Integers, Strings, Floats, etc. An array can also be empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1, 2, 3, 4, 5]
#An array of Integers

words = ["See", "Spot", "run"]
#An array of Strings

mixed = ["hello", 5, true, 3.0]
#An array with a String, Integer, Boolean, and Float

empty = []
#An empty array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Array Index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, each item inside of an array is at a numbered position called an &lt;em&gt;index&lt;/em&gt;. The first item is at index 0, the second item is at index 1, and so on. We can access the &lt;code&gt;i&lt;/code&gt;th element of an array by putting the index in square brackets after invoking the array’s name; this is known as access by &lt;em&gt;index&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example = ["Car", "Boar", 45, 9.9, true]

#For an array named `example`, you can retrieve an item of a particular index by referencing its index.

puts example[2] # =&amp;gt; 45
puts example[0] # =&amp;gt; Car
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Arrays of Arrays&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

multi_d_array.each { |x| puts "#{x}\n" }

#[0, 0, 0, 0]

#[0, 0, 0, 0]

#[0, 0, 0, 0]

#[0, 0, 0, 0]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Hash&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, a &lt;em&gt;hash&lt;/em&gt; is a collection of key-value pairs.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;hash&lt;/em&gt; is denoted by a set of curly braces (&lt;code&gt;{}&lt;/code&gt;) which contains key-value pairs separated by commas. Each value is assigned to a key using a hash rocket (&lt;code&gt;=&amp;gt;&lt;/code&gt;). Calling the &lt;code&gt;hash&lt;/code&gt; followed by a key name within brackets grabs the value associated with that key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;profile = {
  "name" =&amp;gt; "Magnus",
  "profession" =&amp;gt; "chess player",
  "ranking" =&amp;gt; 1,
  "grandmaster?" =&amp;gt; true
}

# "name", "profession", "ranking", and "grandmaster?" are the keys. "Magnus", "chess player", 1 and true are the values.

puts profile["name"] # =&amp;gt; Magnus

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;P.S Hashes are like JS objects. &lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Ruby Hash New&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, a &lt;em&gt;hash&lt;/em&gt; can be created through literal notation (because we are literally assigning what &lt;code&gt;key=&amp;gt;value&lt;/code&gt; pairs we want in the hash) or by assigning a variable equal to &lt;code&gt;Hash.new&lt;/code&gt; which generates a new, empty hash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Creating a hash through literal notation:
lunch = {
  "protein" =&amp;gt; "chicken",
  "greens" =&amp;gt; "lettuce",
  "organic?" =&amp;gt; true
}

#Creating a hash through Hash.new
lunch = Hash.new
puts lunch # =&amp;gt; {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Adding to a Hash&lt;/strong&gt;&lt;br&gt;
We can add to a hash two ways: if we created it using literal notation, we can simply add a new key-value pair directly between the curly braces. If we used &lt;code&gt;Hash.new&lt;/code&gt;, we can add to the hash using bracket notation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pets = Hash.new
pets["Stevie"] = "cat"
# Adds the key "Stevie" with the
# value "cat" to the hash

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Method .Each&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;code&gt;.each&lt;/code&gt; method is used to iterate over arrays and hashes. This allows each element in an array and each key-value pair in a hash to be iterated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#In this example, the each method iterates over every color in the colors array and prints it to the console.

colors = ["red", "blue", "green", "yellow"]

colors.each { |color| puts color }
#Output
#red
#blue
#green
#yellow

#When iterating over hashes, two placeholder variables are needed to represent each key/value pair.

polygons = {
  "pentagon" =&amp;gt; 5,
  "hexagon" =&amp;gt; 6,
  "nonagon" =&amp;gt; 9
}

polygons.each do |shape, sides|
  puts "A #{shape} has #{sides} sides."
end
#Output
#A pentagon has 5 sides.
#A hexagon has 6 sides.
#A nonagon has 9 sides.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Iterating Over Multidimensional Arrays&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

s.each { | sub_array | sub_array.each { | item | puts item} }

#or 

s.each do | sub_array |
  sub_array.each do | y |
    puts y
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Iterating Over Hashes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When iterating over hashes, we need two placeholder variables to represent each key/value pair.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;restaurant_menu = {
  "noodles" =&amp;gt; 4,
  "soup" =&amp;gt; 3,
  "salad" =&amp;gt; 2
}

restaurant_menu.each do |item, price|
  puts "#{item}: #{price}"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Sorting the Hash&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors = { 
  "blue" =&amp;gt; 3,
  "green" =&amp;gt; 1,
  "red" =&amp;gt; 2
}
colors = colors.sort_by do |color, count|
  count
end
colors.reverse!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In the example above, we first create a hash called &lt;code&gt;colors&lt;/code&gt; that maps color strings to numbers.&lt;/li&gt;
&lt;li&gt;Then, we sort &lt;code&gt;colors&lt;/code&gt; into green, red, and blue, from smallest to largest by count. Just so you know, the &lt;code&gt;.sort_by&lt;/code&gt; function returns an array of arrays, but that’s fine for our purposes.&lt;/li&gt;
&lt;li&gt;Finally, we reverse the array order so that the colors with the largest counts are first.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;CREATE A HISTOGRAM&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 { |word, count|
  count }


frequencies.reverse!

frequencies.each { |word, count|
  puts word + " " + count.to_s }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Ruby Loops and Iterators</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Wed, 24 Jan 2024 01:49:07 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/ruby-loops-and-iterators-32b5</link>
      <guid>https://dev.to/aizatibraimova/ruby-loops-and-iterators-32b5</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby while Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Putting a block of code in a while loop in Ruby will cause the code to repeatedly run the code as long as its condition is true.&lt;/p&gt;

&lt;p&gt;If the block of code doesn’t have a way for the condition to be changed to false, the while loop will continue forever and cause an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 1

while i &amp;lt;= 3 do
  puts "Message number #{i}"
  i = i + 1
end

# Output:
# Message number 1
# Message number 2
# Message number 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;The 'Until' Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Putting a block of code inside an &lt;code&gt;until&lt;/code&gt; loop in Ruby will cause the code to run as long as its condition remains &lt;code&gt;false&lt;/code&gt;. It’s only when the condition becomes &lt;code&gt;true&lt;/code&gt; that the loop stops.&lt;/p&gt;

&lt;p&gt;If the block of code doesn’t allow for a way for the condition to be changed to &lt;code&gt;true&lt;/code&gt; then the loop will continue forever and it will cause an error.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 0
until i == 6
  i = i + 1
end
puts i

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby Assignment Operators&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assignment operators in Ruby are used to assign or update values to variables. The most common assignment operator is &lt;code&gt;=&lt;/code&gt; but others also exist, like &lt;code&gt;+=&lt;/code&gt;, &lt;code&gt;-=&lt;/code&gt;, &lt;code&gt;*=&lt;/code&gt; and &lt;code&gt;/=&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 1;
a += 3;
puts a; # Output: 4

b = 4;
b -= 2;
puts b; # Output: 2

num = 12;
num *= 2;
puts num; # Output: 24

num /= 4;
puts num; # Output: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby for Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A block of code can be repeated a set amount of times with the &lt;code&gt;for&lt;/code&gt; loop in Ruby.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in 1..3 do
  puts "Message number #{i}"
end

# Output
# Message number 1
# Message number 2
# Message number 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Inclusive and Exclusive Ranges&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for num in 1...10&lt;/code&gt;. What this says to Ruby is: “For the variable &lt;code&gt;num&lt;/code&gt; in the range &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;10&lt;/code&gt;, do the following.” The following was to puts &lt;code&gt;"#{num}"&lt;/code&gt;, so as &lt;code&gt;num&lt;/code&gt; took on the values of 1 to 9, one at a time, those values were printed to the console.&lt;/p&gt;

&lt;p&gt;The reason this program counted to 9 and not 10 was that we used three dots in the range; this tells Ruby to exclude the final number in the count: &lt;code&gt;for num in 1...10&lt;/code&gt; means “go up to but don’t include 10.” If we use two dots, e.g. &lt;code&gt;for num in 1..10&lt;/code&gt;, this tells Ruby to include the highest number (&lt;code&gt;10&lt;/code&gt;) in the range.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Ruby loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;loop&lt;/code&gt; method can be used to run a block of code repeatedly in Ruby. Either use curly braces (&lt;code&gt;{}&lt;/code&gt;) or the &lt;code&gt;do/end&lt;/code&gt; keyword combination to wrap the block of code that will be looped.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;iterator&lt;/em&gt;&lt;br&gt;
An iterator is just a Ruby method that repeatedly invokes a block of code. The code block is just the bit that contains the instructions to be repeated, and those instructions can be just about anything you like!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 1
loop do
  puts "We are in the loop!"
  num += 1
  break if num &amp;gt; 3
end

puts "We have exited the loop!"

# Output
# We are in the loop!
# We are in the loop!
# We are in the loop!
# We have exited the loop!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;break&lt;/code&gt; keyword is our Get Out of Jail Free card: it breaks a loop as soon as its condition is met.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Next!&lt;/strong&gt;&lt;br&gt;
In Ruby, the &lt;code&gt;next&lt;/code&gt; keyword is used within a loop to pass over certain elements and skip to the following iteration. It is useful for omitting elements that you do not wish to have iterated. &lt;code&gt;next&lt;/code&gt; is followed by an &lt;code&gt;if&lt;/code&gt; statement which defines which elements are to be skipped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in 1..5
  next if i % 2 == 0
  print i
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 20
loop do
  i -= 1
  next if i % 2 == 1
  print "#{i}"
  break if i &amp;lt;= 0
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_array = [1,2,3,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Ruby each Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To iterate over an array in Ruby, use the &lt;code&gt;.each&lt;/code&gt;method. It is preferred over a &lt;code&gt;for&lt;/code&gt; loop as it is guaranteed to iterate through each element of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object.each { |item| 
  # Do something 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the &lt;code&gt;do&lt;/code&gt; keyword instead of &lt;code&gt;{}&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object.each do |item| 
  # Do something 
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data = [3, 6, 9, 12]

data.each do |num|
  puts "The number is: #{num}"
end

# Output:
# The number is: 3
# The number is: 6
# The number is: 9
# The number is: 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;The .times Iterator&lt;/strong&gt;&lt;br&gt;
The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times.&lt;/p&gt;

&lt;p&gt;For example, if we wanted to print out "Chunky bacon!" ten times, we might type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10.times { print "Chunky bacon!" }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;em&gt;Examples:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 1
while i &amp;lt;= 50
  print i
  i -= 1
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 1
until i == 50 do
print i
i += 1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in 1..50
print i 
i += 1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m = 0
loop do 
m += 1
print "Ruby!"
break if m &amp;gt;= 30
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp

words = text.split(" ")

words.each do |word|
  if word != redact
    print word + " "
  else
    print "REDACTED "
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Control Flow in Ruby</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Thu, 18 Jan 2024 16:26:24 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/control-flow-in-ruby-2516</link>
      <guid>https://dev.to/aizatibraimova/control-flow-in-ruby-2516</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if 1 &amp;lt; 2
  print "I'm getting printed because one is less than two!"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby doesn’t care about whitespace (spaces and blank lines), so the indentation of the print statement isn’t necessary. However, it’s a convention that Rubyists (Ruby enthusiasts) follow, so it’s good to get in the habit now. The block of code following an if should be indented two spaces.&lt;/p&gt;

&lt;p&gt;When you’re done with your &lt;code&gt;if&lt;/code&gt;, you have to tell Ruby by typing &lt;code&gt;end.&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Else&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The partner to the &lt;code&gt;if&lt;/code&gt; statement is the &lt;code&gt;else&lt;/code&gt;statement. An &lt;code&gt;if/else&lt;/code&gt;statement says to Ruby: “If this expression is true, run this code block; otherwise, run the code after the &lt;code&gt;else&lt;/code&gt; statement.”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if 1 &amp;gt; 2
  print "I won't get printed because one is less than two."
else
  print "That means I'll get printed!"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Elsif&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if x &amp;lt; y  # Assumes x and y are defined
  puts "x is less than y!"
elsif x &amp;gt; y
  puts "x is greater than y!"
else
  puts "x equals y!"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Unless&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you want to use control flow to check if something is false, rather than if it’s true. You could reverse your if/else, but Ruby will do you one better: it will let you use an unless statement.&lt;/p&gt;

&lt;p&gt;Let’s say you don’t want to eat unless you’re hungry. That is, while you’re not hungry, you write programs, but if you are hungry, you eat. You might write that program in Ruby like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unless hungry
  # Write some sweet programs
else
  # Have some noms
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Equal or Not&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Ruby, we assign values to variables using &lt;code&gt;=&lt;/code&gt;, the assignment operator. But if we’ve already used &lt;code&gt;=&lt;/code&gt; for assignment, how do we check to see if two things are equal? Well, we use &lt;code&gt;==&lt;/code&gt;, which is a comparator (also called a relational operator). &lt;code&gt;==&lt;/code&gt; means “is equal to.” When you type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 2
y = 2
if x == y
  print "x and y are equal!"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you’re saying: “if x equals y, print ‘x and y are equal!’” You can also check to see if two values are not equal using the &lt;code&gt;!=&lt;/code&gt; comparator.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;And&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Comparators aren’t the only operators available to you in Ruby. You can also use logical or boolean operators. Ruby has three: and &lt;code&gt;(&amp;amp;&amp;amp;)&lt;/code&gt;, or &lt;code&gt;(||)&lt;/code&gt;, and not (&lt;code&gt;!&lt;/code&gt;). Boolean operators result in boolean values: true or false.&lt;/p&gt;

&lt;p&gt;The boolean operator and, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, only results in true when both expression on either side of &amp;amp;&amp;amp; are true. Here’s how &amp;amp;&amp;amp; works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true &amp;amp;&amp;amp; true # =&amp;gt; true
true &amp;amp;&amp;amp; false # =&amp;gt; false
false &amp;amp;&amp;amp; true # =&amp;gt; false
false &amp;amp;&amp;amp; false # =&amp;gt; false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Or&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ruby also has the or operator (||). Ruby’s || is called an inclusive or because it evaluates to true when one or the other or both expressions are true. Check it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true || true # =&amp;gt; true
true || false # =&amp;gt; true
false || true # =&amp;gt; true
false || false # =&amp;gt; false


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Not&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, Ruby has the boolean operator not (!). ! makes true values false, and vice-versa.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!true # =&amp;gt; false
!false # =&amp;gt; true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Combining Boolean Operators&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(x &amp;amp;&amp;amp; (y || w)) &amp;amp;&amp;amp; z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;em&gt;Daffy Duckified&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print "Pleathe enter a thtring: " 
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
  user_input.gsub!(/s/, "th")
else
  puts "There are no 's's in your string."
end

puts "Your new thtring is #{user_input}."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the if half of our branch, we want to check whether the user’s input contains an "s".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if string_to_check.include? "substring"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we find "s", we want Ruby to replace every instance of "s" it finds with "th". We can do this with the &lt;code&gt;.gsub!&lt;/code&gt; method, which stands for &lt;strong&gt;g&lt;/strong&gt;lobal &lt;strong&gt;sub&lt;/strong&gt;stitution.&lt;/p&gt;

&lt;p&gt;The syntax looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string_to_change.gsub!(/s/, "th")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Intro to Ruby</title>
      <dc:creator>Aizat Ibraimova</dc:creator>
      <pubDate>Thu, 18 Jan 2024 04:09:20 +0000</pubDate>
      <link>https://dev.to/aizatibraimova/intro-to-ruby-p8p</link>
      <guid>https://dev.to/aizatibraimova/intro-to-ruby-p8p</guid>
      <description>&lt;p&gt;&lt;em&gt;Codecademy Cheatsheet&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Everything in Ruby is an Object
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;'puts' and 'print'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The print command just takes whatever you give it and prints it to the screen. puts (for “put string”) is slightly different: it adds a new (blank) line after the thing you want it to print. You use them like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "What's up?"

print "Oxnard Montalvo"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No parentheses or semicolons needed!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The '.length' Method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"I love espresso".length
# ==&amp;gt; 15 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The '.reverse' Method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Eric".reverse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will result in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"cirE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;'.upcase' &amp;amp; '.downcase'&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "Aizat".upcase

puts "Aizat".downcase
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Multi-Line Comments&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=begin

This is for when

you have a lot of comments to write! 

=end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Strings and String Methods&lt;/strong&gt;&lt;br&gt;
In Ruby, you can do this two ways: each method call on a separate line, or you can chain them together, like this:&lt;/p&gt;

&lt;p&gt;name.method1.method2.method3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = "Jamie"

puts name.downcase.reverse.upcase

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;gets.chomp&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;gets&lt;/code&gt; is the Ruby method that gets input from the user. When getting input, Ruby automatically adds a blank line (or &lt;strong&gt;newline&lt;/strong&gt;) after each bit of input; &lt;code&gt;chomp&lt;/code&gt; removes that extra line. (Your program will work fine without &lt;code&gt;chomp&lt;/code&gt;, but you’ll get extra blank lines everywhere.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable_name = gets.chomp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Formatting with String Methods&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize 
answer.capitalize!


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we introduce one new method, &lt;code&gt;capitalize&lt;/code&gt;, here. It capitalizes the first letter of a string and makes the rest of the letters lower case. We assign the result to &lt;code&gt;answer2&lt;/code&gt;&lt;br&gt;
The next line might look a little strange, we don’t assign the result of &lt;code&gt;capitalize&lt;/code&gt; to a variable. Instead you might notice the &lt;code&gt;!&lt;/code&gt; at the end of capitalize. This modifies the value contained within the variable answer itself. The next time you use the variable answer you will get the results of answer&lt;code&gt;.capitalize&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
