File: index.html.md

package info (click to toggle)
ruby-dry-inflector 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: ruby: 1,498; makefile: 6; sh: 4
file content (72 lines) | stat: -rw-r--r-- 1,927 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: Introduction
description: Standalone inflections
layout: gem-single
order: 18
type: gem
name: dry-inflector
---

dry-inflector is an inflector gem for Ruby, which provides a configurable inflector **object**, rather than using a singleton.

### Basic usage

``` ruby
require "dry/inflector"

inflector = Dry::Inflector.new

inflector.pluralize("book")    # => "books"
inflector.singularize("books") # => "book"

inflector.camelize("dry/inflector")     # => "Dry::Inflector"
inflector.classify("books")             # => "Book"
inflector.demodulize("Dry::Inflector")  # => "Inflector"
inflector.constantize("Dry::Inflector") # => Dry::Inflector

inflector.dasherize("dry_inflector")  # => "dry-inflector"
inflector.underscore("dry-inflector") # => "dry_inflector"

inflector.humanize("dry_inflector") # => "Dry inflector"
inflector.humanize("author_id")     # => "Author"

inflector.tableize("Book")    # => "books"
inflector.foreign_key("Book") # => "book_id"


inflector.ordinalize(1)  # => "1st"
inflector.ordinalize(2)  # => "2nd"
inflector.ordinalize(3)  # => "3rd"
inflector.ordinalize(10) # => "10th"
inflector.ordinalize(23) # => "23rd"
```

### Custom inflection rules

``` ruby
require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.plural      "virus",   "viruses" # specify a rule for #pluralize
  inflections.singular    "thieves", "thief"   # specify a rule for #singularize
  inflections.uncountable "dry-inflector"      # add an exception for an uncountable word
end

inflector.pluralize("virus")     # => "viruses"
inflector.singularize("thieves") # => "thief"

inflector.pluralize("dry-inflector") # => "dry-inflector"
```

### Acronyms

```ruby
require "dry/inflector"

inflector = Dry::Inflector.new do |inflections|
  inflections.acronym "API", "OpenGL"
end

inflector.camelize("opengl_api") # => "OpenGLAPI"
inflector.underscore("OpenGLAPI") # => "opengl_api"
```