<?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>Forem: Carlos Ferrer</title>
    <description>The latest articles on Forem by Carlos Ferrer (@carlosferrerdev).</description>
    <link>https://forem.com/carlosferrerdev</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1167229%2F3b837c8a-0314-43da-be50-7b253b82a864.jpeg</url>
      <title>Forem: Carlos Ferrer</title>
      <link>https://forem.com/carlosferrerdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/carlosferrerdev"/>
    <language>en</language>
    <item>
      <title>Variáveis e Constantes em C#</title>
      <dc:creator>Carlos Ferrer</dc:creator>
      <pubDate>Mon, 16 Oct 2023 21:21:01 +0000</pubDate>
      <link>https://forem.com/carlosferrerdev/variaveis-e-constantes-em-c-1kcp</link>
      <guid>https://forem.com/carlosferrerdev/variaveis-e-constantes-em-c-1kcp</guid>
      <description>&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt; (C Sharp) é uma linguagem de programação orientada a objetos desenvolvida pela Microsoft. Ela oferece suporte a uma ampla gama de recursos que permitem aos desenvolvedores criar aplicativos robustos e escaláveis. Duas das construções fundamentais em C# são &lt;strong&gt;variáveis&lt;/strong&gt; e &lt;strong&gt;constantes&lt;/strong&gt;, que desempenham um papel essencial no armazenamento e gerenciamento de dados em programas. Neste artigo, exploraremos o conceito de variáveis e constantes em C#, suas diferenças, uso adequado e boas práticas para a programação eficaz.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Variáveis em C#&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Uma *&lt;em&gt;variável *&lt;/em&gt;é um espaço na memória do computador destinado a armazenar um valor que pode ser alterado durante a execução do programa. Em C#, as variáveis são declaradas usando um tipo de dados específico, seguido pelo nome da variável. Vejamos um exemplo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Declaração de uma variável 'idade' do tipo int (inteiro)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neste exemplo, declaramos uma variável chamada idade do tipo int, que representa um número inteiro. As variáveis podem ser inicializadas no momento da declaração ou em um momento posterior no código:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idade&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Inicialização da variável 'idade'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aqui, a variável idade é declarada e inicializada com o valor 30.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Tipos de Dados em C#&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;C# oferece uma variedade de tipos de dados que você pode usar para declarar variáveis, incluindo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;int&lt;/strong&gt;: Inteiros.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idade&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;35&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Minha idade é "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;idade&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: Minha idade é 35&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;double&lt;/strong&gt;: Números de ponto flutuante de precisão dupla.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;salario&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2500.75&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Meu salário é R$"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;salario&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: Meu salário é R$2500,75 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;float&lt;/strong&gt;: Números de ponto flutuante de precisão simples.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;altura&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1.75f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Minha altura é "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;altura&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" metros."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: Minha altura é 1.75 metros.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;char&lt;/strong&gt;: Caracteres.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;primeiraLetra&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A primeira letra do meu nome é "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;primeiraLetra&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: A primeira letra do meu nome é C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;string&lt;/strong&gt;: Sequências de caracteres.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;nome&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Carlos"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Meu nome é "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nome&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: Meu nome é Carlos&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;bool&lt;/strong&gt;: Valores booleanos (verdadeiro ou falso).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;temCarro&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tem carro? "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;temCarro&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Saída: Tem carro? True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;E muitos outros, incluindo tipos personalizados.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Constantes em C#&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ao contrário das variáveis, as &lt;strong&gt;constantes&lt;/strong&gt; em C# são valores imutáveis que não podem ser alterados após sua definição. Elas são declaradas usando a palavra-chave const. Vejamos um exemplo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;PI&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3.14159&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Declaração de uma constante 'PI'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neste exemplo, a constante &lt;strong&gt;PI&lt;/strong&gt; é declarada com o valor &lt;code&gt;3.14159&lt;/code&gt; e não pode ser alterada em nenhum ponto do programa. As constantes são frequentemente usadas para representar valores fixos, como constantes matemáticas ou valores que não devem ser modificados durante a execução do programa.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Diferenças entre Variáveis e Constantes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A principal diferença entre &lt;strong&gt;variáveis&lt;/strong&gt; e &lt;strong&gt;constantes&lt;/strong&gt; é que as &lt;strong&gt;variáveis&lt;/strong&gt; podem ser alteradas ao longo do tempo, enquanto as &lt;strong&gt;constantes&lt;/strong&gt; são valores imutáveis. Além disso:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variáveis&lt;/strong&gt; devem ser declaradas usando a palavra-chave &lt;code&gt;var&lt;/code&gt;, seguida do tipo de dado e do nome da variável, enquanto as &lt;strong&gt;constantes&lt;/strong&gt; são declaradas usando a palavra-chave &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variáveis&lt;/strong&gt; podem ser inicializadas no momento da declaração ou posteriormente no código, enquanto as &lt;strong&gt;constantes&lt;/strong&gt; devem ser inicializadas no momento da declaração.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variáveis&lt;/strong&gt; podem ser usadas para armazenar valores que podem mudar, enquanto &lt;strong&gt;constantes&lt;/strong&gt; são usadas para valores que não devem ser alterados.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variáveis&lt;/strong&gt; podem ser usadas em qualquer lugar do código onde seu escopo seja válido, enquanto &lt;strong&gt;constantes&lt;/strong&gt; são acessíveis apenas dentro do escopo em que são definidas.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Boas Práticas&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ao trabalhar com variáveis e constantes em C#, é importante seguir boas práticas para garantir a clareza e a manutenibilidade do código:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Escolha nomes descritivos&lt;/strong&gt;: Dê nomes significativos às suas variáveis e constantes para que o código seja autoexplicativo.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use constantes para valores fixos&lt;/strong&gt;: Se você tem um valor que não deve ser alterado, declare-o como uma constante em vez de uma variável.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mantenha o escopo em mente&lt;/strong&gt;: Lembre-se de que variáveis têm escopo, o que significa que elas só podem ser acessadas em determinadas partes do código.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evite variáveis globais&lt;/strong&gt;: Variáveis globais podem tornar o código mais difícil de entender e depurar. Prefira limitar o escopo das variáveis sempre que possível.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evite variáveis não inicializadas&lt;/strong&gt;: Sempre inicialize suas variáveis antes de usá-las para evitar comportamento inesperado.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evite reatribuir constantes&lt;/strong&gt;: Não tente reatribuir valores a uma constante; isso resultará em erro.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As &lt;strong&gt;variáveis&lt;/strong&gt; e &lt;strong&gt;constantes&lt;/strong&gt; são elementos essenciais em C# que permitem aos programadores armazenar e manipular dados em seus programas. Variáveis são usadas para armazenar valores mutáveis, enquanto constantes são usadas para representar valores imutáveis. Conhecer a diferença entre esses dois conceitos e seguir boas práticas ao usá-los é fundamental para escrever código claro e eficaz em C#.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programacao</category>
    </item>
    <item>
      <title>Mastering Variables and Constants in Ruby: The Beauty of Flexibility and Consistency</title>
      <dc:creator>Carlos Ferrer</dc:creator>
      <pubDate>Thu, 05 Oct 2023 21:50:55 +0000</pubDate>
      <link>https://forem.com/carlosferrerdev/mastering-variables-and-constants-in-ruby-the-beauty-of-flexibility-and-consistency-1cca</link>
      <guid>https://forem.com/carlosferrerdev/mastering-variables-and-constants-in-ruby-the-beauty-of-flexibility-and-consistency-1cca</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xnOSt4Px--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2r6nrcveqlmot5m9psr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xnOSt4Px--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2r6nrcveqlmot5m9psr.png" alt="Image description" width="800" height="787"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby&lt;/strong&gt;, a dynamic, object-oriented programming language, is known for its convenience and simplicity. One of the features that make Ruby so special is the way it treats variables and constants. In this article, we will delve into the depths of this language to understand how Ruby handles these essential elements and how you can elegantly use them in your own projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variables in Ruby: Flexibility that inspires creativity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variables in Ruby are like magic boxes that can contain any type of data — numbers (integers and decimals), strings, objects, and more. The simple act of assigning a value to a variable is known as "assignment". Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Carlos Ferrer"&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; are variables that contain a string and a number respectively. Ruby does not require you to declare the type of variable - it is dynamic and will automatically deduce the type based on the assigned value.&lt;/p&gt;

&lt;p&gt;Another notable aspect is that variables in Ruby begin with lowercase letters or the &lt;code&gt;_&lt;/code&gt; symbol. They are case sensitive, which means that name and Name are different variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constants in Ruby: Stability amid change&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constants&lt;/strong&gt; in Ruby are values that should not be changed during program execution. The decision followed by Ruby developers is to write constants with all letters in capital letters, making them easily distinguishable from variables. Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14159265359&lt;/span&gt;
&lt;span class="no"&gt;DAYS_IN_WEEK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These constants are immutable, and if you try to reassign a value to them, you will receive a warning. However, it is important to note that Ruby does not prevent modifying the contents of objects assigned to a constant. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;MY_ARRAY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;MY_ARRAY&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# This is allowed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scope of Variables and Constants&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ruby also offers different scopes for variables and constants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Local scope&lt;/strong&gt;: Variables defined within a method or block are considered local and can only be accessed within that context. Let's see:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;local_scope_example&lt;/span&gt;
   &lt;span class="n"&gt;my_local_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"This is a local variable"&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;my_local_variable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;local_scope_example&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a local variable&lt;/span&gt;

&lt;span class="c1"&gt;# Attempting to access the variable outside the scope of the method will cause an error.&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;my_local_variable&lt;/span&gt;
&lt;span class="c1"&gt;# Output: NameError: undefined local variable or method 'my_local_variable' for main:Object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Instance scope&lt;/strong&gt;: Instance variables start with &lt;code&gt;@&lt;/code&gt; and are accessible in every instance of a class. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleClass&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
     &lt;span class="vi"&gt;@minha_variavel_instancia&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"This is an instance variable"&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_variable_instance&lt;/span&gt;
     &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@minha_variable_instance&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ExampleClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_variable_instance&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is an instance variable&lt;/span&gt;

&lt;span class="c1"&gt;# Trying to access the instance variable directly outside the class instance will cause an error.&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="vi"&gt;@minha_variable_instance&lt;/span&gt;
&lt;span class="c1"&gt;# Output: SyntaxError: unexpected '@', expecting end-of-input&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class scope:&lt;/strong&gt; Class variables start with &lt;code&gt;@@&lt;/code&gt; and are shared by all instances of a class. Let's see:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleClass&lt;/span&gt;
   &lt;span class="vc"&gt;@@my_variable_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"This is a class variable"&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_variable_class&lt;/span&gt;
     &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vc"&gt;@@my_variable_class&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;object1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ExampleClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;object2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ExampleClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="no"&gt;ExampleClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_variable_class&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a class variable&lt;/span&gt;

&lt;span class="c1"&gt;# Class variables are shared by all instances of the class.&lt;/span&gt;
&lt;span class="n"&gt;object1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_variable_class&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a class variable&lt;/span&gt;
&lt;span class="n"&gt;object2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_variable_class&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a class variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global scope&lt;/strong&gt;: Global variables start with $ and can be accessed anywhere in the code, making them a powerful feature, but one that should be used with caution. Let's see:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="vg"&gt;$my_global_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"This is a global variable"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_global_variable&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vg"&gt;$my_global_variable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;show_global_variable&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a global variable&lt;/span&gt;

&lt;span class="c1"&gt;# Global variables can be accessed anywhere in the code.&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vg"&gt;$my_global_variable&lt;/span&gt;
&lt;span class="c1"&gt;# Output: This is a global variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The elegance of flexibility and consistency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ruby, with its simplicity and flexibility, makes programming a creative and elegant experience. Variables and constants play a crucial role in this journey, allowing developers to express their ideas in an intuitive and consistent way.&lt;br&gt;
By understanding the difference between variables and constants in Ruby, as well as the scopes in which they operate, you will be prepared to write elegant and effective Ruby code. Enjoy the beauty of this language and let your creativity flow in the art of programming.&lt;br&gt;
I hope this article has been an enlightening introduction to the world of variables and constants in Ruby. As you deepen your knowledge, you will discover even more reasons to fall in love with this versatile and expressive language. &lt;em&gt;&lt;strong&gt;Happy coding!&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
