Last active
October 2, 2019 18:06
-
-
Save eftakhairul/2404934 to your computer and use it in GitHub Desktop.
Some snippets of Ruby on Knowledge Sharing Session -Ruby at Mangoes Mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
author Eftakhairul Islam <[email protected]> | |
website http://eftakhairul.com | |
=end | |
#My First Program at Ruby. | |
puts 'Hello World' | |
puts ("I'm here") | |
#Deffirent between puts and print | |
puts ('Here I am again') #Instant flush, $stdout right away And newline automatically. | |
print ("Here I am again") #Buffering the input, | |
#Exmaple | |
5.times { puts "Hello"; sleep 2 } | |
5.times { print "Hello "; sleep 2 } | |
STDOUT.sync = true #You can use STDOUT.flush as well. | |
5.times { print "hello "; sleep 2 } | |
1.upto(10) {|i| print i } | |
('a'..'f').each {|i| print i } | |
#Common Mathematical terms | |
=begin | |
+ plus | |
- minus | |
/ slash | |
* asterisk | |
% percent | |
< less-than | |
> greater-than | |
<= less-than-equal | |
>= greater-than-equal | |
=end | |
#Exmaple | |
puts "Is it greater?", 5 > -2 | |
puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 | |
puts 42.even? | |
#Variables | |
cars = 100 | |
puts "There are #{cars} cars available." | |
language = 'Ruby' | |
my_height = 74 | |
my_weight = 180 # lbs | |
puts "Let's talk about %s." % language | |
puts "He's %d inches tall." % my_height | |
puts "If I add %d, %d then I will get %d." % [ | |
my_height, my_weight, my_height + my_weight] | |
puts "-" * 50 | |
#Some interesting string operations | |
greeting = "Hello Everyone!" | |
greeting.delete('l') #Output: Heo Everyone! | |
t2 = "sample,data,from,a,CSV" | |
t2.split(",") # | |
hello".gsub("ll","y yo") #output: hey yoo | |
#Array and Hashes | |
data = ['cat', 'dog', 'ant', 'bee'] | |
data.push('bird') | |
puts data[0] | |
#print the whole array | |
puts data.inspect | |
#Hash This is also same to array...In php and ruby array works as hash and array both. Basically, they are diffeenrt in other language | |
#Same as above | |
data = %w{ ant bee cat dog } | |
data = { | |
1 => 'dog', | |
2 => 'cat', | |
3 => 'bee', | |
4 => 'ant' | |
} | |
puts data.inspect | |
restaurantMenu = { "Burger" => 3, "Pizza" => 10, "Coffee" => 2 } | |
#print key and value | |
restaurantMenu.each do | item, price | | |
puts "#{item}: $#{price}" | |
end | |
#return all keys | |
puts restaurantMenu.keys | |
#another way to create hash | |
my_hash = Hash.new | |
my_hash['burger'] = 3 | |
print my_hash['burger'] #output will be 3 | |
#Loop | |
#normal while | |
i = 0; | |
num = 5; | |
while i < num do | |
puts("Inside the loop i = #{i}" ); | |
i +=1; | |
end | |
#do while | |
i = 0; | |
num = 5; | |
begin | |
puts("Inside the loop i = #{i}" ); | |
i +=1; | |
end while i < num | |
#for | |
for i in 1..5 | |
puts "Value of local variable is #{i}" | |
end | |
#each | |
(0..5).each do |i| | |
puts "Value of local variable is #{i}" | |
end | |
(0..5).each {|i| puts "Value of local variable is #{i}"} | |
family = { "Homer" => "dad", | |
"Marge" => "mom", | |
"Lisa" => "sister", | |
"Maggie" => "sister", | |
"Abe" => "grandpa", | |
"Santa's Little Helper" => "dog" | |
} | |
family.each { |x, y| puts "#{x}: #{y}" } #it prints both key and value | |
until counter == 0 | |
puts "The counter is #{counter}" | |
counter = counter - | |
end | |
#Regular Expression | |
line = 'Perl' | |
if line =~ /Perl|Python/ #Basically regular expression by writing a pattern between slash characters (/pattern/) | |
puts "Scripting language mentioned: #{line}" | |
end | |
#Introduction to Function | |
def printCurrentTime() | |
time = Time.new | |
return time.ctime | |
end | |
def printName( name ) | |
puts "My name is: #{name.capitalize}." | |
end | |
def printAllNames( *args ) | |
arg1, arg2 = args | |
puts "Name-1: #{arg1}, Name-2: #{arg2}" | |
end | |
puts printCurrentTime() | |
printName( "Eliza" ) | |
printAllNames( "Tahins", "Eftakhairul" ) | |
#OOP | |
class BookInStock | |
@isbn | |
@price | |
@@count = 0 | |
attr_reader :price | |
def initialize(isbn, price) | |
@isbn = isbn | |
@price = Float(price) | |
@@count += 1 | |
end | |
def price=(newPrice) | |
@price = newPrice | |
end | |
def isbn | |
@isbn | |
end | |
private | |
def printName | |
puts 'My Name is Ruby lolz!!! Stupid I am not human' | |
end | |
public | |
def to_s | |
"ISBN: #{@isbn}, price: #{@price}" | |
end | |
def printAll | |
printName | |
end | |
def self.printCount() | |
puts "Box count is : #@@count" | |
end | |
end | |
book = BookInStock.new('BN123', 12) | |
book.price = 10 | |
puts book.price | |
puts book.to_s | |
puts book.printAll | |
book2 = BookInStock.new('BC100', 12) | |
book3 = BookInStock.new('BD200', 12) | |
BookInStock.printCount() | |
class Box | |
BOX_COMPANY = "BD Box Ltd." | |
def initialize(w,h) | |
@width, @height = w, h | |
end | |
def getArea | |
@width * @height | |
end | |
end | |
# define a subclass | |
class BigBox < Box | |
# add a new instance method | |
def printArea | |
@area = @width * @height | |
puts "Big box area is : #@area" | |
end | |
end | |
# create an object | |
box = BigBox.new(10, 20) | |
# print the area | |
box.printArea() | |
puts "Company Name: #{BigBox::BOX_COMPANY}" | |
#Diff btw @@ and @ | |
@@ -> class variable; It's available through all classess and extended class. it's kind of java's static variable | |
@ -> instance variable; It's only available class's lexical scope. | |
#Taking input from commandline | |
STDOUT.flush | |
print "How old are you? " | |
age = gets.chomp() | |
puts "So, you're #{age} old." | |
first, second, third = ARGV | |
puts "The script is called: #{$0}" | |
puts "Your first variable is: #{first}" | |
puts "Your second variable is: #{second}" | |
puts "Your third variable is: #{third}" | |
#Importing library | |
require 'open-uri' | |
open("http://eftakhairul.com") do |f| | |
f.each_line {|line| p line} | |
end | |
#Open, read AND write file | |
if File.exists?('hello.txt') | |
txt = File.open('hello.txt') | |
puts txt.read() | |
else | |
puts 'File not found.' | |
end | |
#More details @ http://ruby-doc.org/core-1.9.3/File.html | |
#Debuging function | |
abort("Message goes here") | |
#OR | |
puts "Message goes here" | |
exit | |
#Another best way to debug | |
raise Object.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment