Rails 7 on Jruby 9.4.0.0

Step to do :

  1. Install Jruby
  2. run jruby -S gem install rails
  3. run jruby -S rails new App
  4. Step 3 will fail miserably related to bindex gem.
  5. Open Gemfile and comment out web-console gem.
  6. Go to your App folder and run jruby -S bundle install
  7. run jruby -S rails server
  8. Browse localhost:3000 to view rails welcome page.

Ruby Lambda Operator

A lambda is a way to define a block and its parameter with special syntax.

greet1 = -> { puts “Hello world.” }

greet2 = lambda { puts “Hello world !” }

greet1.call

greet2.call

greet_with_param = ->(name) { puts “Hello #{name}!” }

greet_with_param.call(“Joni”)

Ruby spaceship operator <=>

The spaceship operator is general comparison operator. The <=> operator compares 2 values and returning -1, 0, or 1 depending on whether the first value is less than, equal, or greater than the second.

Example in irb.

This operator is used on ruby range operation , objects must implement <=> and succ method to perform range operation. Lets test the a numeric value for these methods.

Keep learning ruby on 2021.

Ruby upto for looping

Numeric variable on ruby have upto method that we can use to do looping. Normally looping is done by using while or for syntax. Ruby can do differently.

Example above we loop from 10 to 15. Its simpler right.

Shorthand If and unless in Ruby lang

The basic conditional if in ruby is like any other programming language.

if condition then

[statements to execute if condition is satisfied (true) ]

end

If only a statement needed to executed then we can use shorthand form of if. Statement is at front followed by if and condition.

Ruby also have unless for conditional branching. Difference is statement will be executed if condition not satisfied (false).

Try it yourself at irb ! Bye …

Ruby Programming : Constant

Ruby programming language have constant like other language we know ? Or Ruby dont have ?

Ruby have naming convention related to variable names. If a variable name begin with upper case then its considered constant. Ruby wont stop you to change that constant, it only emit warning like a test case below on irb.

So thats Ruby’s constant.

Gem Install Location

This is an interesting command to know. We can use gem environment to find out gem files install location. Below is sample output on windows OS.