a shorter way to do `class year def self.hi puts "hi" end end`? in ruby

There is a shorter way to define class methods in Ruby using the class << self syntax. Here's how you can rewrite your code using this syntax:

main.rb
class Year
  class << self
    def hi
      puts "hi"
    end
  end
end
72 chars
8 lines

This syntax allows you to define methods that can be called directly on the class itself, without the need for an instance of the class. So you can call Year.hi to output "hi".

related categories

gistlibby LogSnag