💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › subjects › science-and-technology › compu… captured on 2024-05-12 at 15:34:53.
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
# Computer Science Cheatsheet ## Programming Concepts ### Variables and Data Types Variables hold data and data types define the kind of data a variable can hold.
x = 10
string = "hello"
integer = 42
float = 3.14
boolean = true
array = [1, 2, 3]
hash = { name: "John", age: 25 }
# Functions Functions are reusable code blocks that take input parameters and perform a set of instructions.
def greet(name)
puts "Hello, #{name}!"
end
greet("John")
# Loops Loops allow you to repeatedly execute code.
i = 0
while i < 5 do
puts i
i += 1
end
for i in 0..4
puts i
end
[1, 2, 3].each do |i|
puts i
end
# Conditionals Conditionals execute code based on whether a condition is true or false.
x = 10
if x > 5
puts "x is greater than 5"
end
if x < 5
puts "x is less than 5"
else
puts "x is greater than or equal to 5"
end
grade = "A"
case grade
when "A"
puts "Great job!"
when "B"
puts "Good job!"
else
puts "Try harder next time!"
end
# File Manipulation Reading and writing files is a common task in programming.
File.open("file.txt", "w") do |file|
file.write("Hello, world!")
end
File.open("file.txt", "r") do |file|
puts file.read
end
Resources - [Ruby Documentation](https://www.ruby-lang.org/en/documentation/) - [Ruby on Rails](https://rubyonrails.org/) - [Learn Ruby the Hard Way](https://learnrubythehardway.org/) - [Ruby Monk](https://rubymonk.com/)