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.

Ruby interaction with command line

Ruby have a way to execute command line program using back tick. Enclosed our statement to be executed by command line using back tick.

Lets try using back tick on irb !

we can put the output to a variable to process it like sample below.

ls is a command to list content of your file system, if you wonder …

bye and good night …

Alternative Syntax of Ruby Hashes

Hashes variant 1

cars = { “car1” => “avanza”, “car2” => “innova”, “car3” => “mirage” }

Hashes variant 2

cars = {  :car1 => “avanza”,  :car2 => “innova”,  :car3 => “mirage” }

Hashes variant 3

cars = {  car1: “avanza”,  car2: “innova”,  car3: “mirage” }

 

These are as far as i know.

Bluefish editor for programming

If you a programmer you can use vi or vim or nano to code. Sometimes it just not enough feature or give comfort. I use an editor called bluefish. Install it from package like this :

For openbsd do pkg_add bluefish

If installing for openbsd dont forget to set your PKG_PATH env variable.

For Dragonfly BSD do pkg install bluefish

Bluefish have a good support for html tags and ruby language. See bluefish screenshot below.

Happy coding all !

 

Ruby Unique Feature – Block

Block is an interesting ruby languange feature. Block is statements enclosed together using do end or curly braces {}. We can find block in action at code that iterating  an array variable.

Lets try block on irb (interactive ruby)  !!!

Sample above creating variable x which is an array of string. Next command iterating the array using each method and a block passed to it. The block have a parameter named n and n will contain each array element passed to it by each method. Code inside block print output to screen.

Below is an example of block using curly braces.

That is simple examples of block in ruby language. It is an unique and usefull feature.

Bye for now !

Ruby Unique Feature – Open Classes

Open Classes

A class in ruby can be modified by adding method or variable at any point in the code.

Sample code as follow :

class ClassA
  def method1
   puts '1'
  end
end

var1 = ClassA.new
var1.method1

class ClassA
 def method2
 puts '2'
 end
end

var1.method2
var1.method1

 

Ruby Unique Feature – Hashes

Hashes

A hash is a collection of key and value pairs. Use curly braces {} to define a hash. Use brackets [] to get a value of a key from a hash.

Define a hash like below

nations = { ‘ID’ => ‘Indonesia’ , ‘MY’ => ‘Malaysia’, ‘SG’ => ‘Singapore’ }

To add a key value pair to hash

nations[‘BN’] = ‘Brunei Darussalam’

To output value of a key in hash

puts nations[‘ID’]

To output a hash entirely

puts nations

Bye for now !