Introduction to Ruby Programming Language



Introduction

This is a quick overview about Ruby programming languages that should take no more time to test and check flavour for Ruby. It makes the assumption that you already have Ruby installed. (If you do not have Ruby on your computer install it before you get started.)


Interactive Ruby

Just like python IDLE(Interactive shell ), Ruby also comes with Interactive Ruby where you can run and see the results of any Ruby statements. Playing with Ruby code in interactive sessions like this is a terrific way to learn the language.


Open up IRB (which stands for Interactive Ruby).

  • If you’re using macOS open up Terminal and type irb, then hit enter.

  • If you’re using Linux, open up a shell and type irb and hit enter.

  • If you’re using Windows, open Interactive Ruby from the Ruby section of your Start Menu.


When you will type irb in terminal your terminal screen will be look something like this


Now It's time to check some basic code test of Interactive Ruby,


Type this : "Hello World"

This is output of "Hello World" In Ruby.

If we want to print out “Hello World” we need a bit more:



puts is the basic command to print something out in Ruby.


But then what’s the => nil bit?


That’s the result of the expression. puts always returns nil, which is Ruby’s absolutely-positively-nothing value



Now, Let perform some basic arithmetic operation in IRB


in Ruby ** is the way you say “to the power of”. But what if you want to go the other way and find the square root of something?


Math is a built-in module for mathematics. Modules serve two roles in Ruby. This shows one role: grouping similar methods together under a familiar name. Math also contains methods like sin() and tan().



define a method in Ruby



The code def hi starts the definition of the method. It tells Ruby that we’re defining a method, that its name is hi.


The next line is the body of the method, the same line we saw earlier: puts "Hello Codersarts".


Finally, the last line end tells Ruby we’re done defining the method.


Ruby’s response => :hi tells us that it knows we’re done defining the method. This response could be => nil for Ruby 2.0 and earlier versions. But, it’s not important here, so let’s go on.



Now, Calling the function in Ruby


irb(main):013:0> hi
Hello World!
=> nil
irb(main):014:0> hi()
Hello World!
=> nil

Well, that was easy. Calling a method in Ruby is as easy as just mentioning its name to Ruby. If the method doesn’t take parameters that’s all you need. You can add empty parentheses if you’d like, but they’re not needed.


What if we want to say hello to one person, and not the whole world? Just redefine hi to take a name as a parameter.


irb(main):028:0> def hi(name)
irb(main):029:1> puts "Hello "+ name
irb(main):030:1> end
=> :hi
irb(main):031:0> hi "Codersarts"
Hello Codersarts
=> nil
irb(main):032:0> hi("Codersarts")
Hello Codersarts
=> nil
irb(main):033:0> 



Inserting something into a string

irb(main):015:0> def hi(name)
irb(main):016:1> puts "Hello #{name}!"
irb(main):017:1> end
=> :hi
irb(main):018:0> hi("Matz")
Hello Matz!
=> nil

What’s the #{name} bit?


That’s Ruby’s way of inserting something into a string. The bit between the braces is turned into a string (if it isn’t one already) and then substituted into the outer string at that point



Create class in Ruby

irb(main):024:0> class Greeter
irb(main):025:1>   def initialize(name = "World")
irb(main):026:2>     @name = name
irb(main):027:2>   end
irb(main):028:1>   def say_hi
irb(main):029:2>     puts "Hi #{@name}!"
irb(main):030:2>   end
irb(main):031:1>   def say_bye
irb(main):032:2>     puts "Bye #{@name}, come back soon."
irb(main):033:2>   end
irb(main):034:1> end
=> :say_bye

The new keyword here is class. This defines a new class called Greeter and a bunch of methods for that class.


Also notice @name. This is an instance variable, and is available to all the methods of the class. As you can see it’s used by say_hi and say_bye


Create class object

irb(main):035:0> greeter = Greeter.new("Pat")
=> #<Greeter:0x16cac @name="Pat">
irb(main):036:0> greeter.say_hi
Hi Pat!
=> nil
irb(main):037:0> greeter.say_bye
Bye Pat, come back soon.
=> nil

Once the greeter object is created, it remembers that the name is Pat.


There are a lot of new things in Ruby, so this is quick introduction later we'll see other such blog.



15 views0 comments

Recent Posts

See All