• Ruby Lambdas Example

    lambdas allow you to define a block

    greeting = -> { "Hello, world!" }
    

    lambdas are anonymous when they are not assigned to a variable

    -> (x) { x + 1 }
    

    lambdas aren’t executed when they are defined, they are executed then they are called

    add_one = -> (x) { x + 1 }
    add_one.call(9) # 10
    

    lambdas can be called a couple different ways

    add_one.(9) # 10
    add_one[9] # 10
    

    example use

    Define a lambda to use a custom method with map

      add_one = -> (x) { x + 1 }
      [10, 20, 30].map(&add_one) # [11, 21 31]
    

    Chain lambdas together (currying) for a functional style

    add_one = -> (x) { x + 1 }
    double_up = -> (x) { x * 2}
    decorate = -> (x) { " -#{x}- " }
    all = add_one >> double_up >> decorate
    [10, 20, 30].map(&all) # [" -22- ", " -42- ", " -62- "]
    

    with parameters

    lambdas can take parameters as well.

    yoda_answer = lambda do |string|
      if string == "try"
        return "There is no such thing" 
      else
        return "Do or do not."
      end
    end
    puts yoda_answer.call("try") # There is no such thing.
    puts yoda_answer.call("do") # Do or do not.
    

    references

    https://www.honeybadger.io/blog/using-lambdas-in-ruby/

    https://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/

    https://www.rubymonk.com/learning/books/1-ruby-primer/chapters/34-lambdas-and-blocks-in-ruby/lessons/77-lambdas-in-ruby

    Stabby lambda syntax was introduced in Ruby 1.9

    02 Mar 2020
  • How to Call Rails Helper in Rails Console

    Remember to prefix the method with helper. to run in rails console.

    Given a helper method like this:

    module PostHelper
      def markdown(text)
        options = [
          no_intra_emphasis: true, 
          fenced_code_blocks: true,
          gh_blockcode: true,   
          disable_indented_code_blocks: true,
          prettify: true
        ]
        markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, *options)
        markdown.render(text).html_safe
      end
    end
    

    To test in rails console try this:

    $ rails console
    Loading development environment (Rails 6.0.1)
    [1] pry(main)> helper.markdown("Hello, world!")
    => "<p>Hello, world!</p>\n"
    [2] pry(main)>
    

    28 Feb 2020
  • Markdown Code Example

    To insert highlight code inside of a post, it’s enough to use some specific tags, has directly described into the Jekyll documentation. In this way the code will be included into a .highlight CSS class and will be highlight according to the syntax.scss file. This is the standard style adopted by Github to highlight the code.

    This is a CSS example:

    body {
      background-color: #fff;
      }
    
    h1 {
      color: #ffaa33;
      font-size: 1.5em;
      }

    And this is a HTML example, with a linenumber:

    1
    2
    3
    
    <html>
      <a href="example.com">Example</a>
    </html>
    

    Last, a Ruby example:

    1
    2
    3
    
    def hello
      puts "Hello World!"
    end
    

    31 Dec 2019