PWA enabled website

  1. Create your website code.
  2. Create manifest.json in the home directory with below code.
{
  "name": "My One Page PWA",
  "short_name": "PWA",
  "description": "A simple one-page PWA",
  "start_url": "/index.html",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#000000",
  "icons": [
    {
      "src": "icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

3. Link manifest.josn in your project index file.

<link rel="manifest" href="/manifest.json">

4. Create a service-worker.js file.

self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open('my-cache').then((cache) => {
      return cache.addAll([
        '/',
        '/index.html',
        '/stylesheet.css',
        '/app.js',
        '/icons/img-192x192.png',
        '/icons/img-512x512.png'
      ]);
    })
  );
});

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      return response || fetch(event.request);
    })
  );
});

5. Add the Register the service worker code to your main JavaScript file ( i.e. app.js)

if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/service-worker.js')
      .then((registration) => {
        console.log('Service worker registered:', registration);
      })
      .catch((error) => {
        console.error('Service worker registration failed:', error);
      });
  });
}

6. Host your website in HTTPS

7. Test your website via Chrome’s Lighthouse or the Application panel in Chrome DevTools.

Cheers!

How Ruby Developer can learn Blockchain development?

Question: I am a 10 year experienced ruby developer, I want to work in blockchain which language will I have to learn

Answer:

As Ruby (widely used for Rails development) is not used directly for Blockchain development. You will have to learn a language which is more compatible with Blockchain development. i.e. Solidity, Golang, C++ Or Node.js


So, you will likely need to learn Solidity for developing smart contracts on the Ethereum blockchain.

Solidity is a high-level programming language that is similar to JavaScript and is designed specifically for writing smart contracts that run on the Ethereum Virtual Machine (EVM) It is a statically typed language that supports inheritance, libraries, and complex user-defined types.

Solidity is not the only language used in blockchain development, but it is the most widely used for smart contract development on the Ethereum platform. Other blockchain platforms may use different languages, such as Golang for the Hyperledger Fabric blockchain, or C++ for the EOS blockchain.

While your experience with Ruby will not directly translate to Solidity or other blockchain languages, your existing programming knowledge and experience should help you learn and adapt quickly. You may want to start by studying Solidity‘s syntax, data types, and control structures, as well as its specific features related to blockchain development such as interacting with the blockchain, managing transactions and handling errors.

Get started Learning BlockChain

Learn Solidity Programming Language: This is designed for developing smart contracts that run on Ethereum. Click here to get started with Solidity.

Learn Flat and Relational DB

Learn Data Structure: i.e. Stack, LinkedList, and Queue.

Learn Terms related with blockchain ie. Mining, Public Distributed Ledger, Hash Encryption, Proof of work etc.


Learn How does Ethereum, HyperLedger, Hashgraph works?

Understand the Economics behind blockchain:

  1. Cryptography and Economics
  2. Cryptography in blockchain invoices hashing, Digital Signatures, and Proof of Work
  3. Hashing uses a cryptography algorithm using (sha256 in bitcoin and ethash in ethereum)
  4. Digital Signature: Private Key and Public Key.
  5. POW requires a miner to solve a mathematical puzzle to be able to add a block to the blockchain

Start Coding and Creating your own smart contract.Create own distributed application in Ethereum.

How to Write Awesome code – Ruby on Rails – V1

Everyone wish to write the better code, But How, Is there any pattern / behaviour? Can anyone point to your code what can be improved?

The answer is YES, There is pattern you should follow the OOPS way by it’s core. I have written SOLID principle before this, Please check this too. I am going to write more series in this topic as it’s really interesting and helpful to everyone.

focus on one thing
Focus on one thing in a Class

In Ruby it’s damn easy but we don’t understand what mistake we are doing, so chances of improvement is less. So, Let’s take an example of below MyClass in code1 as below.

Code1:

class MyClass
  attr_reader :input
  
  def initialize(input)
    @input = input
  end

  def double
    case input
      when Numeric  then target . 2 
      when String   then target.next    # lazy fail ex
      when Array    then target.collect {|o| MyClass.new(o).double}
      else
        raise “don’t know how to double #{input.class} #{input.inspect}”
    end
  end
end

The result of the code1 is Output1 as below:

>> MyClass.new(‘aaa’).double
=> "aab"
>> MyClass.new(49).double
=> 98
>> MyClass.new([‘b’, 6]).double
=> ["c", 12]
>> MyClass.new({‘x’=>‘y’}).double
RuntimeError: don’t know how to double Hash {"x"=>"y"}
        from (irb):73:in `double'
        from (irb):80
        from :0

The above code looks Good, Right ?

You’re probably used / gone thought with this pattern. and, Its everywhere in Ruby on Rails.

But, This style of code is Absolutely Wrong and that you should do a different thing.

Why is the problem in above code?

If I change how double works on any of these classes, MyClass must change, but that’s not the real problem. What happens if MyClass wants to double some new kind of object? I have to go into MyClass and add a new branch to the case statement. How annoying is that?

But that’s the least of it. If I’m writing code that follows this pattern, I likely have many classes that do stuff based on the classes of their collaborators. My entire application behaves this way. Every time I add a new collaborating object I have to go change code everywhere. Each subsequent change makes things worse. My application is a teetering house of cards and eventually it will come tumbling down.

Also, what if some other wishes to use MyClass with their new SuperDuperClass object? They can’t reuse MyClass without changing it since MyClass has a very limited notion of what kinds of objects can be doubled.

MyClass is both rigid and closed for extension.

Code2:

class Numeric
  def double
    self * 2
  end
end

class String
  def double
    self.next 
  end
end

class Array
  def double
    collect {|e| e.double}
  end
end

class Object
  def double
    raise "don't know how to double #{self.class} #{self.inspect}"
  end
end

class MyClass
  attr_reader :input
  
  def initialize(input)
    @input = input
  end

  def double
    input.double
  end
end

What basically it should be as below (Something like this) :

Using this new code, Output1 will be the same as before, but now we can also use this way as output2:

Output2:

>> 'aaa'.double
=> "aab"
>> 49.double
=> 98
>> ['b', 6].double
=> ["c", 12]

In this above code2,

The Objects are what they are and because of that, they actually behave the way they do.

Above statement is very simple but incredibly important.

They tell each other what, not how.

What is the event/notification/request and it is the responsibility of the sender.(sender should know).

How is the behaviour/implementation and it should be completely hidden in the receiver (sender not required to know).

Therefore:

  • MyClass should not know how Numeric implements double.
  • MyClass should just ask target to double itself.
  • All possible targets must implement double.
  • Object should implement double to help you get your Ducks in a row.

Thanks for reading. For more information I highly recommend to read Sandi Metz books.

Courtesy 99 Bottles of OOP by Sandi Metz.

Sandi metz

Happy Learning & Coding!

Note: This concept can be used with any programming language.

SOLID Principle in Ruby on Rails

SOLID Stands for:

  • Single responsibility principle
  • Open/closed principle
  • Liskov substitution principle
  • Interface segregation principle
  • Dependency inversion principle

The acronym stands for principles to help software engineers to create / write maintainable source code for long-term that use an OOPS language.

Single responsibility principle: Focus on One thing in a Class Seprately.

Open/closed principle: Class should be open for extension and closed for modification.

 Liskov substitution principle: The derived class must be capable of being substituted by its base class.

Interface Segregation principle:

Interface Segregation Principle (1996)

Dependency Inversion principle (DIP): Now a days used with Docker, Docker composer.

The Dependency Inversion Principle