Coder Social home page Coder Social logo

rubyrefactorings's Introduction

Icon

RubyRefactorings

Build Downloads

A RubyMine plugin that provides additional refactorings for the Ruby language, implemented using code intentions.

Some refactorings that are available when installing the plugin:

  • Replace singleton method by opening singleton class:

    Replace singleton method by opening singleton class example

  • Remove unnecessary braces from hash argument:

    Remove unnecessary braces from hash argument example

  • Introduce string interpolation:

    Introduce interpolation example

  • Convert single-quoted string to double-quoted string:

    Change a single-quoted string literal to have double quotes example

  • Replace conditional with guard clause:

    Simplify if chain by introducing guard clauses

    Introduce guard clause in conditional spanning a whole method

    Introduce guard clase in block

    Introduce guard clase replacing elsifs that include exceptions

  • Extract method object (a.k.a. Replace Function with Command):

    Extract method object example

  • Convert string/symbol word list (using %w/%i syntax) to array (using [] syntax):

    Convert %w and %i literals to square bracket array

  • Split map/collect/each by introducing chained map:

    Split map in two different ways

    Split each, and collect with brace block

  • Remove useless conditional statement

    Replace if statement with true condition by its then block

    Replace if expression with false condition by its else block

    Replace if expression with false condition and no else block by nil

    Replace unless expression with multiple statements by block

  • Move into conditional above

    Move a statement into all branches of a conditional that is just before it

  • Use self-assignment

    Replace assignment to increment numeric variable by self-assignment

    Replace short-circuit or to memoize result of computation by self-assignment

    Replace hash access to increment value by self-assignment

rubyrefactorings's People

Contributors

javiergelatti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rubyrefactorings's Issues

Request: Struct Definition to Class Definition

I pretty regularly build out classes as structs first, just because it can go a lot faster initially, and makes testing easy.

However, I usually need to expand these from just data structs into full-fat classes to start adding methods to them, which is pretty tedious.

So, the transform would look like this:

NetworkLayer = Struct.new "NetworkLayer" :next_layer, :nodes

Which would become this:

class NetworkLayer                       # name of class taken from first arg passed to Struct#new
    attr_accessor :next_layer, :nodes    # accessors for each kwarg that went into Struct#new
    
    def initialize(next_layer:, nodes:)  # same kwargs as Struct#new...
        @next_layer = next_layer         # ... which are then assigned to instance vars of the same name
        @nodes = nodes
    end
end

Happy to try making a PR if you think that's interesting!

Add refactor: introduce interpolation

Let's say we have the following string

'Current year: 2021'

I would like to have a refactor to

current_year_number = 2021
"Current year: #{current_year_number}"

Note the double quote and the extract variable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.