Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify rbs syntax to code snippets in Markdown files #1366

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ We define the standard members order so that ordering doesn't bother reading dif
5. `public` & public instance methods
6. `private` & private instance methods

```
```rbs
class HelloWorld[X]
def self.new: [A] () { (void) -> A } -> HelloWorld[A] # new or initialize comes first
def initialize: () -> void
Expand Down
51 changes: 16 additions & 35 deletions docs/rbs_by_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ The purpose of this doc is to teach you how to write RBS signatures by using the

## Examples

In each example, the first snippet is for *Ruby* and the second one is for *RBS*.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] I thought # .rb and # .rbs in snippets are redundant, so I've removed them and added the description above.


### Zero argument methods

**Example:** `String#empty?`

```ruby
# .rb
"".empty?
# => true
"hello".empty?
# => false
```

```ruby
# .rbs
```rbs
class String
def empty?: () -> bool
end
Expand All @@ -32,14 +32,13 @@ end
**Example:** `String#include?`

```ruby
# .rb
"homeowner".include?("house")
# => false
"homeowner".include?("meow")
# => true
```

```ruby
```rbs
class String
def include?: (String) -> bool
end
Expand All @@ -53,7 +52,6 @@ boolean value
**Example:** `String#end_with?`

```ruby
# .rb
"hello?".end_with?("!")
# => false
"hello?".end_with?("?")
Expand All @@ -64,8 +62,7 @@ boolean value
# => false
```

```ruby
# .rbs
```rbs
class String
def end_with?: (*String) -> bool
end
Expand All @@ -79,7 +76,6 @@ returns a boolean value.
**Example:** `String#ljust`

```ruby
# .rb
"hello".ljust(4)
#=> "hello"
"hello".ljust(20)
Expand All @@ -88,8 +84,7 @@ returns a boolean value.
#=> "hello123412341234123"
```

```ruby
# .rbs
```rbs
class String
def ljust: (Integer, ?String) -> String
end
Expand All @@ -102,7 +97,6 @@ end
**Example:** `Array#*`

```ruby
# .rb
[1, 2, 3] * ","
# => "1,2,3"
[1, 2, 3] * 2
Expand All @@ -112,8 +106,7 @@ end
*Note:* Some of the signatures after this point include type variables (e.g. `Elem`, `T`).
For now, it's safe to ignore them, but they're included for completeness.

```ruby
# .rbs
```rbs
class Array[Elem]
def *: (String) -> String
| (Integer) -> Array[Elem]
Expand All @@ -128,16 +121,14 @@ end
**Example:** `String#<<`

```ruby
# .rb
a = "hello "
a << "world"
#=> "hello world"
a << 33
#=> "hello world!"
```

```ruby
# .rbs
```rbs
class String
def <<: (String | Integer) -> String
end
Expand All @@ -148,7 +139,6 @@ end
### Nilable types

```ruby
# .rb
[1, 2, 3].first
# => 1
[].first
Expand All @@ -159,8 +149,7 @@ end
# => []
```

```ruby
# .rbs
```rbs
class Enumerable[Elem]
def first: () -> Elem?
| (Integer) -> Array[Elem]
Expand All @@ -183,7 +172,6 @@ The `?` syntax is a convenient shorthand for a union with nil. An equivalent uni
**Example**: `String#lines`

```ruby
# .rb
"hello\nworld\n".lines
# => ["hello\n", "world\n"]
"hello world".lines(' ')
Expand All @@ -192,8 +180,7 @@ The `?` syntax is a convenient shorthand for a union with nil. An equivalent uni
# => ["hello", "world"]
```

```ruby
# .rbs
```rbs
class String
def lines: (?String, ?chomp: bool) -> Array[String]
end
Expand All @@ -209,12 +196,11 @@ Keyword arguments are declared similar to in ruby, with the keyword immediately
**Example**: `Time.now`

```ruby
# .rb
Time.now
# => 2009-06-24 12:39:54 +0900
```

```ruby
```rbs
class Time
def self.now: () -> Time
end
Expand All @@ -228,16 +214,14 @@ end
**Example**: `Array#filter`

```ruby
# .rb
[1,2,3,4,5].filter {|num| num.even? }
# => [2, 4]
%w[ a b c d e f ].filter {|v| v =~ /[aeiou]/ }
# => ["a", "e"]
[1,2,3,4,5].filter
```

```ruby
# .rbs
```rbs
class Array[Elem]
def filter: () { (Elem) -> boolish } -> ::Array[Elem]
| () -> ::Enumerator[Elem, ::Array[Elem]]
Expand All @@ -260,8 +244,7 @@ h.keys
# => ["a", "b", "c", "d"]
```

```ruby
# .rbs
```rbs
class Hash[K, V]
def keys: () -> Array[K]
end
Expand All @@ -273,16 +256,14 @@ Generic types in RBS are parameterized at declaration time. These type variables


```ruby
# .rb
a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!"}
# => ["a!", "b!", "c!", "d!"]
a.collect.with_index {|x, i| x * i}
# => ["", "b", "cc", "ddd"]
```

```ruby
# .rbs
```rbs
class Array[Elem]
def collect: [U] () { (Elem) -> U } -> Array[U]
| () -> Enumerator[Elem, Array[untyped]]
Expand All @@ -302,7 +283,7 @@ In this example, the method receives its signature from the inferred return type
# => [[2, 4, 6], [1, 3, 5]]
```

```ruby
```rbs
class Enumerable[Elem]
def partition: () { (Elem) -> boolish } -> [Array[Elem], Array[Elem]]
| () -> ::Enumerator[Elem, [Array[Elem], Array[Elem] ]]
Expand All @@ -318,7 +299,7 @@ Tuples can be of any size, and they can have mixed types.
# => {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
```

```ruby
```rbs
class Enumerable[Elem]
def to_h: () -> ::Hash[untyped, untyped]
| [T, U] () { (Elem) -> [T, U] } -> ::Hash[T, U]
Expand Down
2 changes: 1 addition & 1 deletion docs/repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Assume there is a rubygem called `bug-free-doodle` and our application depends o

One workaround is to add type definitions of the library in the application signatures.

```
```rbs
# sig/polyfill/bug-free-doodle.rbs

module Bug
Expand Down
14 changes: 7 additions & 7 deletions docs/sigs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ See [syntax guide](syntax.md).
When you finish writing signature, you may want to test the signature.
rbs provides a feature to test your signature.

```
```console
$ RBS_TEST_TARGET='Foo::*' bundle exec ruby -r rbs/test/setup test/foo_test.rb
```

Expand Down Expand Up @@ -74,7 +74,7 @@ The `rbs` test framework tries to the best error message for overloaded methods

The error is reported when a method is defined multiple times, as RBS does not allow duplicate method definitions. When you need to overload a method, use the `...` syntax:

```ruby
```rbs
# First definition
class C
def foo: () -> untyped
Expand All @@ -99,14 +99,14 @@ The design of the signature testing aims to be non-intrusive. The setup is done
You need to require `rbs/test/setup` for signature testing.
You can do it using `-r` option through command line argument or the `RUBYOPT` environment variable.

```
```console
$ ruby -r rbs/test/setup run_tests.rb
$ RUBYOPT='-rrbs/test/setup' rake test
```

When you are using Bundler, you may need to require `bundler/setup` explicitly.

```
```console
$ RUBYOPT='-rbundler/setup -rrbs/test/setup' bundle exec rake test
```

Expand All @@ -130,7 +130,7 @@ You need to specify `RBS_TEST_TARGET` to run the test, and you can customize the
You may need to specify `-r` or `-I` to load signatures.
The default is `-I sig`.

```
```shell
RBS_TEST_OPT='-r pathname -I sig'
```

Expand All @@ -144,7 +144,7 @@ You can see the backtrace how the type error is caused and debug your program or

So, a typical command line to start the test would look like the following:

```
```console
$ RBS_TEST_LOGLEVEL=error \
RBS_TEST_TARGET='Kaigi::*' \
RBS_TEST_SKIP='Kaigi::MonkeyPatch' \
Expand All @@ -160,7 +160,7 @@ $ RBS_TEST_LOGLEVEL=error \

You can skip installing the instrumentation per-method basis using `rbs:test:skip` annotation.

```
```rbs
class String
%a{rbs:test:skip} def =~: (Regexp) -> Integer?
end
Expand Down
4 changes: 2 additions & 2 deletions docs/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ We support writing tests for core/stdlib signatures.

First, execute `generate:stdlib_test` rake task with a class name that you want to test.

```bash
```console
$ bundle exec rake 'generate:stdlib_test[String]'
Created: test/stdlib/String_test.rb
```
Expand Down Expand Up @@ -83,7 +83,7 @@ If the execution of the program escape from the class definition, the instrument

You can run the test with:

```
```console
$ bundle exec rake stdlib_test # Run all tests
$ bundle exec ruby test/stdlib/String_test.rb # Run specific tests
```
Loading