Ruby
Ruby is a high-level dynamically typed language that is similar to Python.
Getting Started
Start up the Ruby REPL (irb
) and enter the following:
def hi
puts "Hello World!"
end
Now you should be able to write hi
and it should output Hello World!
and return nil
. If a function has no parameters, you can call it by name; otherwise, use brackets. I think brackets are a good idea all of the time to remain explicit.
A class is made similarly:
class Greeter
def initialize(name = "World")
@name = name
end
def say_hi
puts("Hello #{@name}!")
end
def say_bye
puts("Bye, #{@name}, see you soon.")
end
end
To instatiate this, you can call Greeter.new("Bob")
or whatever name, and assign it to a variable. Then you can call the methods of this class instance from the variable. e.g. new_greeter.say_hi()
.
Classes can be modified after ther are defined, and these changes are retroactively propagated through all instances, since it is using the class as a reference.
Array Methods
Since I use these all the time and always forget which does what, I put the analogous Javascript method here for sake of lookup.
JS Method | Ruby Method | Example |
---|---|---|
forEach | each | `[1,2,3].each { |
map | map | `[1,2,3].map { |
filter | select | `[1,2,3].select { |
reduce | inject | `[1,2,3].inject(0) { |
References
- https://www.ruby-lang.org/en/
- https://www.ruby-lang.org/en/documentation/quickstart/
- https://en.wikipedia.org/wiki/Ruby_(programming_language)
- https://www.railstutorial.org/
- https://ruby-doc.com/docs/ProgrammingRuby/
- http://sinatrarb.com/
- https://learnxinyminutes.com/docs/ruby/
- http://www.rubykoans.com/
- https://upskillcourses.com/courses/essential-web-developer-course
- https://thoughtbot.com/upcase
- https://rubymonk.com/learning/books
- https://piped.kavin.rocks/watch?v=B3Fbujmgo60
- https://www.learnenough.com/ruby-on-rails-7th-edition-tutorial
- https://scribe.rip/retention-science/ruby-is-still-a-diamond-b789d2661266
- https://poignant.guide/
- https://sorbet.org/
Last modified: 202401040446