Today I learned – Ruby : Data Types and Variables

Hello fellow programmers!

Before diving into Ruby fundamentals, I’d like to share a bit of background on the purpose of this post. I’m a bootcamp graduate who learned Ruby and Ruby on Rails during the program. Since then, I’ve explored various program…


This content originally appeared on DEV Community and was authored by Shraddha

Hello fellow programmers!

Before diving into Ruby fundamentals, I'd like to share a bit of background on the purpose of this post. I'm a bootcamp graduate who learned Ruby and Ruby on Rails during the program. Since then, I've explored various programming languages, including Java and Python. While actively seeking a full-time software engineering position, I recently had the chance to revisit my Ruby skills for an interview. This post is my attempt to review and document these concepts once again.

Data Types

Ruby : strings, numbers, nil, booleans, arrays, and hashes

Strings

fruit = "strawberries"
puts "#{fruit} are rich in antioxidants"

Don't use backticks for strings in Ruby

String methods

Instance method vs Class method

  • When a method starts with a # in documentation, it's an instance method
  • When a method starts with a . in documentation, it's a class method

Numbers

  • Integers are whole numbers, like 7.
  • Floats are decimal numbers, like 7.3.
7.5.floor
# => 7
7.5.ceil
# => 8
10.next
# => 11

Convert some other data types to integers or floats with the #to_i and #to_f methods:

"1".to_i
# => 1
"1.1".to_i
# => 1
"1.1".to_f
# => 1.1

Nil

Represents the absence of a value, nil.

puts "I return nil"
# I return nil
# => nil

Ruby won't let you create a variable without assigning a value. You must explicitly assign a value of nil if you want an "empty" variable:

no_value
# NameError (undefined local variable or method `no_value' for main:Object)
no_value = nil
# => nil

Booleans

Only nil and false are falsy values. Everything else is truthy, even 0 and empty strings.

!!true
# => true
!!false
# => false
!!1
# => true
!!0
# => true
!!""
# => true
!!nil
# false

Symbol

A symbol is a representation of a piece of data. Symbols look like this :my_symbol. You write symbols by placing a : in front of the symbol name:

:this_is_a_symbol

:my_symbol.object_id
# => 2061148
:my_symbol.object_id
# => 2061148
"my string".object_id
# => 200
"my string".object_id
# => 220

Arrays

Array class for storing ordered lists of data.

[1, 3, 400, 7]
# => [1, 3, 400, 7]

You can also create an array with the Array.new syntax. Just typing Array.new will create an empty array ([]):

Array.new
# => []
[1, 3, 400, 7].length
# => 4
[5, 100, 234, 7, 2].sort
# => [2, 5, 7, 100, 234]
[1, 2, 3].reverse
# => [3, 2, 1]

Hashes

You can create a hash by simply writing key/value pairs enclosed in curly braces:

{ key1: "value1", key2: "value2" }

To access data from this hash, you can use the bracket notation and pass in the symbol for the key you are trying to access:

my_hash = { key1: "value1", key2: "value2" }
my_hash[:key2]
# => "value2"

You can also create hashes with Strings for keys:

{ "i'm a key" => "i'm a value!", "key2" => "value2" }

You can also use the Hash.new syntax, which would create an empty hash, {}:

Hash.new
# => {}

Resources

Ruby documentation

Coming up next - Methods and Parameters


This content originally appeared on DEV Community and was authored by Shraddha


Print Share Comment Cite Upload Translate Updates
APA

Shraddha | Sciencx (2024-07-19T03:25:32+00:00) Today I learned – Ruby : Data Types and Variables. Retrieved from https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/

MLA
" » Today I learned – Ruby : Data Types and Variables." Shraddha | Sciencx - Friday July 19, 2024, https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/
HARVARD
Shraddha | Sciencx Friday July 19, 2024 » Today I learned – Ruby : Data Types and Variables., viewed ,<https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/>
VANCOUVER
Shraddha | Sciencx - » Today I learned – Ruby : Data Types and Variables. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/
CHICAGO
" » Today I learned – Ruby : Data Types and Variables." Shraddha | Sciencx - Accessed . https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/
IEEE
" » Today I learned – Ruby : Data Types and Variables." Shraddha | Sciencx [Online]. Available: https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/. [Accessed: ]
rf:citation
» Today I learned – Ruby : Data Types and Variables | Shraddha | Sciencx | https://www.scien.cx/2024/07/19/today-i-learned-ruby-data-types-and-variables/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.