💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › programming-languages › ruby.md captured on 2024-08-18 at 18:18:47.
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
# Ruby Cheatsheet ## Unique Features - Dynamic, object-oriented language - Interpreted with garbage collection - Duck typing - Blocks and Procs - Mixins - RubyGems package manager ## Variables
x = 5
@name = "John"
@@count = 0
$debug = true
## Functions
def say_hello(name)
puts "Hello, #{name}!"
end
say_hello("Ruby")
## Loops
while x < 10 do
puts x
x += 1
end
for i in 0..5
puts i
end
(1..5).each do |i|
puts i
end
## Conditionals
if x > 5
puts "x is greater than 5"
elsif x == 5
puts "x is equal to 5"
else
puts "x is less than 5"
end
x > 5 ? "x is greater than 5" : "x is less than or equal to 5"
## File Manipulation
file = File.open("filename.txt", "r")
contents = file.read
file.close
File.open("filename.txt", "w") do |file|
file.write("Hello, world!")
end
## Resources - [Ruby Documentation](https://www.ruby-lang.org/en/documentation/) - [RubyGems](https://rubygems.org/) - [Ruby on Rails](https://rubyonrails.org/)