I tried to write code like a type declaration in Ruby

Conclusion

The following code will work.

n = int 42
# =>42 is substituted

f = int 4.2
# => TypeError!

What i did

Monkey patch to the Kernel module as follows

module Kernel
    module_function
        
    def int(var = 0)
        if var.is_a?(Integer)
            var
        else
           raise TypeError, "#{var} isn't Integer"
        end
    end 
end

After that, you can create a Ruby variable like a type declaration just by writing n = int 42. Also, if you pass a value of a different type (or class), TypeError will occur as an exception.

n = int 42
i = int 21

p n
# => 42
p i
# => 21

n = int 4.2
# => `int': 4.2 isn't Integer (TypeError)

If you want to narrow the range of influence, you should write as follows in refinements

module Type
    refine Kernel do
       module_function
        
       def int(var = 0)
           if var.is_a?(Integer)
               var
           else
              raise TypeError, "#{var} isn't Integer"
           end
       end
    end
end

After that, if you use ʻusing Type` where you want to use it, it's OK.

in conclusion

For the time being, ʻIntegerandString` can be written like a type declaration like this. Let's think about how to write Array or Hash.

Recommended Posts

I tried to write code like a type declaration in Ruby
I tried to convert a string to a LocalDate type in Java
I tried to make a parent class of a value object in Ruby
I tried to create a Clova skill in Java
I tried to make a login function in Java
I want to create a Parquet file even in Ruby
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I wrote a C parser (like) using PEG in Ruby
I wrote a code to convert numbers to romaji in TDD
I tried to create a simple map app in Android Studio
I tried to implement Ajax processing of like function in Rails
I tried to illuminate the Christmas tree in a life game
[Ruby] I want to put an array in a variable. I want to convert to an array
[Ruby 3.0] A memo that I added a type definition to a library I wrote
I tried to make Numeron which is not good in Ruby
I got stuck trying to write a where in clause in ActiveRecord
I want to write a nice build.gradle
I tried migrating Processing to VS Code
I want to write a unit test!
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried embedding a formula in Javadoc
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[iOS] I tried to make a processing application like Instagram with Swift
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to build a Firebase application development environment with Docker in 2020
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I thought about the best way to create a ValueObject in Ruby
I tried to make full use of the CPU core in Ruby
I tried to make a talk application in Java using AI "A3RT"
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
A memorandum to clean up the code Ruby
I tried to organize the session in Rails
I want to use arrow notation in Ruby
java I tried to break a simple block
Code to escape a JSON string in Java
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
[Ruby] I want to do a method jump!
I tried to implement deep learning in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
I tried to create a LINE clone app
I made a Ruby extension library in C
[Ruby basics] I tried to learn modules (Chapter 1)
How to write code that thinks object-oriented Ruby
How to implement a like feature in Rails
I tried to output multiplication table in Java
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I want to get the value in Ruby
I tried to easily put CentOS-7 in a PC that I no longer need
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
Do something like a JS immediate function in Ruby