Git Product home page Git Product logo

simplified-ruby's Introduction

Simplified Ruby

What is it?

Simplified Ruby is a minimal subset of the Ruby syntax that allows to take any problem description and turn it into idiomatic, easy to understand and boring Ruby code.

Motivation

Ruby has a lot of features, quirks, and ways to solve the same problem. I believe that you can take 20% of its syntax and still be able to write code that does the job. This is especially true when you're using a framework (like Ruby on Rails) and a lot of technical problems are being handled for you.

Limiting yourself to Simplified Ruby shifts the focus from technical problems (exciting, but your company probably won't earn money for solving them) to business domain problems (here's your real leverage). You end up with code that is easy to read, easy to understand for your colleagues regardless of their seniority, and easy to delete.

This project can also be useful:

  • when introducing people to programming
  • to answer the question "how much Ruby syntax do I need to start writing Ruby on Rails applications?"
  • during technical interviews for introductory Ruby positions
  • for company career ladder to define "basic Ruby knowledge"

Syntax

simple types - String, Symbol, Fixnum, Float, nil, booleans

"adam"
:adam
56
3.4
nil
true
false

method call - .

"adam".upcase

arthimetical operators - +, -, *, /, **, %

2 + 5.4
23 - 10
10 * 2
9 / 2
2 ** 3
8 % 3

string interpolation

"this is #{4}."

comparison operators

2 + 2 == 4
3 != 4

logical operators - &&, ||, !

"adam".length.zero? && 2 + 2 == 4
"adam".length.zero? || 2 + 2 == 4
!"adam".length.zero?

conditional statements - if / elsif / else

if "adam".length.zero?
  "awesome"
elsif 2 + 2 == 4
  3
else
  "something else"
end

"test" if 2 + 2 == 4

container types - Array, Hash

[1, 2, 3]
{ "age" => 3 }

short-hand notation for Hash with symbol keys

{ age: 3 }

local variables

name = "adam"

assignment operator

age = 2 + 3
age = "five"

defining methods

def my_method(argument)
  "I got #{argument}"
end

return

def my_method(argument)
  return "a" if argument.zero?

  "bbb"
end

keyword arguments

def my_method(argument, name:, age: 18, city: nil)
end

comments

# this is how it works

blocks

[1, 2].map do |element|
  element * 2
end

defining a class

class User
end

new

user = User.new("adam")

instance method

class User
  def name
    "adam"
  end
end

instance variable

class User
  def give_name(name)
    @name = name
  end

  def name
    @name
  end
end

initialize

class User
  def initialize(name)
    @name = name
  end
end

private

class User
  def introduce
    "I am #{age}"
  end

  private

  def age
    4
  end
end

self

class User
  def car
    Car.new(self)
  end
end

class method

class User
  def self.give_me_three
    [User.new, User.new, User.new]
  end
end

constant inside a class

class User
  CODE_LENGTH = 3

  def code(name)
    name.slice(0, CODE_LENGTH)
  end
end

inheritance - <

class User
  def roles
    []
  end
end

class Supervisor < User
  def roles
    ["admin", "supervisor"]
  end
end

super

class Supervisor < User
  def code(name)
    "#{super("ADMIN")} #{super}"
  end
end

nested classes

class Users
  class Admin
  end
end

Users::Admin.new

attr_reader, attr_writer, attr_accessor

class User
  attr_reader :name
  attr_writer :ago
  attr_accessor :city
end

||=

class User
  def give_name(name)
    @name ||= name
  end
end

begin / rescue / raise

begin
  2 + 2
  raise "error"
  4 * 7
rescue => ex
  "Aaaa #{ex}!!!"
end

Basic methods

simplified-ruby's People

Contributors

adamniedzielski avatar

Stargazers

Marc avatar Paul Senior avatar Rashedul Islam Riyad avatar Martin Calveira avatar Sven Winkler avatar Adam Daniels avatar Juri Hahn avatar Tim Riley avatar João Marcos avatar Oleg Sovetnik avatar Marco Roth avatar Alexander Brandon Coles avatar Peter Solnica avatar Carsten Zimmermann avatar

Watchers

Carsten Zimmermann avatar James Cloos avatar  avatar  avatar

Forkers

rubyworkout

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.