twitter: tuxdna
1 $ sudo yum install ruby ruby-irb rubygems # installation on Fedroa / RHEL
2 $ ruby my-script.rb # running a ruby script
3 $ irb # interactive ruby
4 $ gem install wirble # optional
1 name = "Matz" # local variable
2 age = 46 # local variable
3 $debug = true # global variable
4 @id = 10 # instance variable
5 @dept = "Engineering" # instance variable
6 @@total_instances = 10 # class variable
7 MyClass # class name
8 LOG_LEVEL = 10 # constant
9 var.object_id # everything is an object
10 :north # symbol
11 [1,2,3,4] # array
12 {:age => 46, :name => "matz" } # hash
1 puts LOG_LEVEL
2 print "Hello"
3 p "Object"
4 printf "My name is %s. I am %d years old.", 'matz', 46
5 line = gets
1 grade = gets.chomp
2 if grade == "A"
3 "excellent"
4 elsif grade == "B"
5 "good"
6 else
7 "no one can save you now!"
8 end
9 # while loop
10 input = ""
11 while input != "quit"
12 input = gets.chomp
13 puts input
14 end
15 # statement modifier
16 puts "not a valid end of loop" if input != "quit"
17 puts "Never say die!" while true
18 # switch-case construct
19 grade = gets.chomp
20 case grade
21 when "A", "B"
22 puts "Pass"
23 else
24 puts "Fail"
25 end
1 /Perl (is|isn't) good/ # /regex/
2 r = Regexp.new("Perl (is|isn't) good")
3 str1 = "Perl is good"
4 str2 = "Perl isn't good"
5 str1 =~ /Perl (is|isn't) good/ # => 0
6 r.match(str2) # => #<MatchData "Perl isn't good" 1:"isn't">
7 md = r.match(str2)
8 input = gets.chomp
9 puts "Matched" if input =~ /Perl (is|isn't) good/
1 # check whether a string is a palindrome or not
2 def palindrome?(s)
3 a = s.gsub(/[\W]/, "").downcase
4 return true if a.reverse == a
5 return false
6 end
7
8 # factorial of a number
9 def fact(number)
10 if number < 0
11 raise "Invalid input"
12 end
13 if number > 1
14 return number*fact(number-1)
15 else
16 return number
17 end
18 end
1 class Person
2 @@total_instances = 0
3 def initialize(name, address)
4 @name = name
5 @address = address
6 @@total_instances += 1
7 end
8 def say_hello
9 printf "I am #{@name}, and I live at: #{@address}"
10 end
11 def Person.how_many
12 @@total_instances
13 end
14 end
15 class Employee < Person
16 def init
17 end
1 class Employee < Person
2 def initialize(name, address, company)
3 super(name, address)
4 @company = company
5 end
6 def say_hello
7 super
8 printf "I work at %s.\n", @company
9 end
10 end
11
12 p1 = Person.new("Saleem", "Magarpatta")
13 p2 = Person.new("Matz", "Japan")
14 p1.say_hello
15 p2.say_hello
16 printf "Persons instantiated so far: %d\n\n", Person.how_many
17
18 e1 = Employee.new("Rambo", "Italy", "Hollywood")
19 e1.say_hello
20 printf "Persons instantiated so far: %d\n\n", Person.how_many
1 require 'person'
2 module College
3 class Student < Person
4 end
5 end
6 e4 = College::Student.new("Messi", "Argentina")
7 e4.say_hello
8 printf "Persons instantiated so far: %d\n\n", Person.how_many
1 class Person
2 include Comparable
3 def <=>(other)
4 self.name.length <=> other.name.length
5 end
6 end
1 # gem install <gem-name>
2 # gem list
Programming Ruby Ruby Global Variables Ruby Introductory Tutorial Ruby Symbols Ruby Comparable Interface
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |