Today I learned - Ruby : Data Types and Variables
Shraddha
Posted on July 19, 2024
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
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
Coming up next - Methods and Parameters
Posted on July 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.