SlideShare a Scribd company logo
1 of 14
Learn RUBY Programming at
AMC Square Learning
•An interpreted language
• a.k.a dynamic, scripting
• e.g., Perl
•Object Oriented
• Single inheritance
•High level
• Good support for system calls, regex and CGI
•Relies heavily on convention for syntax
Hello World
#!/usr/bin/env ruby
puts “Hello world”
$ chmod a+x helloWorld.rb
$ helloWorld.rb
Hello world
$
• shell script directive to run ruby
• Needed to run any shell script
• Call to method puts to write out
“Hello world” with CR
• Make program executable
Basic Ruby
•Everything is an object
•Variables are not typed
•Automatic memory allocation and garbage
collection
•Comments start with # and go to the end of the line
• You have to escape # if you want them elsewhere
•Carriage returns mark the end of statements
•Methods marked with def … end
Control structures
If…elsif…else…end
case when <condition> then
<value>… else… end
unless <condition> … end
while <condition>… end
until <condition>… end
#.times (e.g. 5.times())
#.upto(#) (e.g. 3.upto(6))
<collection>.each {block}
• elsif keeps blocks at same level
• case good for checks on multiple
values of same expression; can use
ranges
grade = case score
when 90..100 then “A”
when 80..90 then “B”
else “C”
end
• Looping constructs use end (same as
class definitions)
• Various iterators allow code blocks to
be run multiple times
Ruby Naming Conventions
• Initial characters
• Local variables, method parameters, and method names 
lowercase letter or underscore
• Global variable  $
• Instance variable  @
• Class variable  @@
• Class names, module names, constants  uppercase letter
• Multi-word names
• Instance variables  separate words with underscores
• Class names  use MixedCase
• End characters
• ? Indicates method that returns true or false to a query
• ! Indicates method that modifies the object in place rather than
returning a copy (destructive, but usually more efficient)
Another Example
class Temperature
Factor = 5.0/9
def store_C(c)
@celsius = c
end
def store_F(f)
@celsius = (f - 32)*Factor
end
def as_C
@celsius
end
def as_F
(@celsius / Factor) + 32
end
end # end of class definition
Factor is a constant
5.0 makes it a float
4 methods that get/set an
instance variable
Last evaluated statement is
considered the return
value
Second Try
class Temperature
Factor = 5.0/9
attr_accessor :c
def f=(f)
@c = (f - 32) * Factor
end
def f
(@c / Factor) + 32
end
def initialize (c)
@c = c
end
end
t = Temperature.new(25)
puts t.f # 77.0
t.f = 60 # invokes f=()
puts t.c # 15.55
attr_accessor creates setter and
getter methods automatically for a
class variable
initialize is the name for a
classes’ constructor
Don’t worry - you can always override
these methods if you need to
Calls to methods don’t need () if
unambiguous
Input and Output - tsv files
f = File.open ARGV[0]
while ! f.eof?
line = f.gets
if line =~ /^#/
next
elsif line =~ /^s*$/
next
else
puts line
end
end
f.close
ARGV is a special array
holding the command-
line tokens
Gets a line
If it’s not a comment or a
blank line
Print it
Processing TSV filesh = Hash.new
f = File.open ARGV[0]
while ! f.eof?
line = f.gets.chomp
if line =~ /^#/
next
elsif line =~ /^s*$/
next
else
tokens = line.split /t/
h[tokens[2]] = tokens[1]
end
end
f.close
keys =
h.keys.sort {|a,b| a <=> b}
keys.each {|k|
puts "#{k}t#{h[k]}" }
Declare a hash table
Get lines without n or rn - chomp
split lines into fields delimited with tabs
Store some data from each field into the
hash
Sort the keys - sort method takes a block
of code as input
each creates an iterator in which k is set
to a value at each pass
#{…} outputs the evaluated expression in
the double quoted string
Blocks
•Allow passing chunks of code in to methods
•Receiving method uses “yield” command to call
passed code (can call yield multiple times)
•Single line blocks enclosed in {}
•Multi-line blocks enclosed in do…end
•Can use parameters
[ 1, 3, 4, 7, 9 ].each {|i| puts i }
Keys = h.keys.sort {|a,b| a <=> b }
Running system commands
require 'find'
Find.find('.') do
|filename|
if filename =~ /.txt$/i
url_output =
filename.gsub(/.txt$/i, ".html")
url = `cat #{filename}`.chomp
cmd = "curl #{url} -o #{url_output}";
puts cmd
`#{cmd}`
end
end
• require reads in another
ruby file - in this case a
module called Find
• Find returns an array, we
create an iterator filename
to go thru its instances
• We create a new variable to
hold a new filename with the
same base but different .html
extension
• We use backticks `` to run a
system command and
(optionally) save the output
into a variable
• curl is a command in mac os to
retrieve a URL to a file, like wget in
unix
CGI example
require 'cgi'
cgi = CGI.new("html3")
size = cgi.params.size
if size > 0 # processing form
in = cgi.params['t'].first.untaint
cgi.out { cgi.html { cgi.head
cgi.body { "Welcome, #{in}!" }
} }
else
puts <<FORM
Content-type: text/html
<HTML><BODY><FORM>
Enter your name: <INPUT TYPE=TEXT
NAME=t><INPUT TYPE=SUBMIT>
</FORM></BODY></HTML>
FORM
end
• CGI requires library
• Create CGI object
• If parameters passed
• Process variable t
• untaint variables if using
them in commands
• No parameters?
• create form using here
document “<<“
Reflection
...to examine aspects of the program from within the program itself.
#print out all of the objects in our system
ObjectSpace.each_object(Class) {|c| puts c}
#Get all the methods on an object
“Some String”.methods
#see if an object responds to a certain method
obj.respond_to?(:length)
#see if an object is a type
obj.kind_of?(Numeric)
obj.instance_of?(FixNum)
Thank You

More Related Content

What's hot

CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxSam Bowne
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesP3 InfoTech Solutions Pvt. Ltd.
 
CNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeCNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeSam Bowne
 
scala-gopher: async implementation of CSP for scala
scala-gopher:  async implementation of CSP  for  scalascala-gopher:  async implementation of CSP  for  scala
scala-gopher: async implementation of CSP for scalaRuslan Shevchenko
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.pptKalkey
 
Triton and symbolic execution on gdb
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdbWei-Bo Chen
 
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginCNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginSam Bowne
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminarygo-lang
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16Max Kleiner
 

What's hot (20)

Klee and angr
Klee and angrKlee and angr
Klee and angr
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
 
CNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeCNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: Shellcode
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
 
Python
PythonPython
Python
 
scala-gopher: async implementation of CSP for scala
scala-gopher:  async implementation of CSP  for  scalascala-gopher:  async implementation of CSP  for  scala
scala-gopher: async implementation of CSP for scala
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
Triton and symbolic execution on gdb
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdb
 
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginCNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you Begin
 
Defer, Panic, Recover
Defer, Panic, RecoverDefer, Panic, Recover
Defer, Panic, Recover
 
Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
 
x86
x86x86
x86
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
Clojure 7-Languages
Clojure 7-LanguagesClojure 7-Languages
Clojure 7-Languages
 

Similar to Learn Ruby Programming in Amc Square Learning

shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptxNiladriDey18
 
Mastering unix
Mastering unixMastering unix
Mastering unixRaghu nath
 
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfOrtus Solutions, Corp
 
The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5Wim Godden
 
Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)Damien Seguy
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot CampTroy Miles
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Ahmed El-Arabawy
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing DaeHyung Lee
 
React Native One Day
React Native One DayReact Native One Day
React Native One DayTroy Miles
 

Similar to Learn Ruby Programming in Amc Square Learning (20)

shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Gun make
Gun makeGun make
Gun make
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Designing Ruby APIs
Designing Ruby APIsDesigning Ruby APIs
Designing Ruby APIs
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
 
The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5
 
Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)Preparing for the next PHP version (5.6)
Preparing for the next PHP version (5.6)
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
Slides
SlidesSlides
Slides
 
C#unit4
C#unit4C#unit4
C#unit4
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 

More from ASIT Education

COMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSCOMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSASIT Education
 
Simple hardware problems facing in pc's
Simple hardware problems facing in pc'sSimple hardware problems facing in pc's
Simple hardware problems facing in pc'sASIT Education
 
Amc square IT services
Amc square IT servicesAmc square IT services
Amc square IT servicesASIT Education
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.ASIT Education
 
Learn my sql at amc square learning
Learn my sql at amc square learningLearn my sql at amc square learning
Learn my sql at amc square learningASIT Education
 
Learn joomla at amc square learning
Learn joomla at amc square learningLearn joomla at amc square learning
Learn joomla at amc square learningASIT Education
 
learn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learninglearn ANDROID at AMC Square Learning
learn ANDROID at AMC Square LearningASIT Education
 
Learn spring at amc square learning
Learn spring at amc square learningLearn spring at amc square learning
Learn spring at amc square learningASIT Education
 
Learn cpp at amc square learning
Learn cpp at amc square learningLearn cpp at amc square learning
Learn cpp at amc square learningASIT Education
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learningASIT Education
 
learn sharepoint at AMC Square learning
learn sharepoint at AMC Square learninglearn sharepoint at AMC Square learning
learn sharepoint at AMC Square learningASIT Education
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testingASIT Education
 
C programmimng basic.ppt
C programmimng basic.pptC programmimng basic.ppt
C programmimng basic.pptASIT Education
 
Introduction to vm ware
Introduction to vm wareIntroduction to vm ware
Introduction to vm wareASIT Education
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testingASIT Education
 
Introduction to phython programming
Introduction to phython programmingIntroduction to phython programming
Introduction to phython programmingASIT Education
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingASIT Education
 
Introduction to internet
Introduction to internetIntroduction to internet
Introduction to internetASIT Education
 

More from ASIT Education (20)

COMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSCOMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETS
 
Simple hardware problems facing in pc's
Simple hardware problems facing in pc'sSimple hardware problems facing in pc's
Simple hardware problems facing in pc's
 
Amc square IT services
Amc square IT servicesAmc square IT services
Amc square IT services
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Learn my sql at amc square learning
Learn my sql at amc square learningLearn my sql at amc square learning
Learn my sql at amc square learning
 
Learn joomla at amc square learning
Learn joomla at amc square learningLearn joomla at amc square learning
Learn joomla at amc square learning
 
learn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learninglearn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learning
 
Learn spring at amc square learning
Learn spring at amc square learningLearn spring at amc square learning
Learn spring at amc square learning
 
Learn cpp at amc square learning
Learn cpp at amc square learningLearn cpp at amc square learning
Learn cpp at amc square learning
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
learn sharepoint at AMC Square learning
learn sharepoint at AMC Square learninglearn sharepoint at AMC Square learning
learn sharepoint at AMC Square learning
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testing
 
C programmimng basic.ppt
C programmimng basic.pptC programmimng basic.ppt
C programmimng basic.ppt
 
Introduction to vm ware
Introduction to vm wareIntroduction to vm ware
Introduction to vm ware
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testing
 
Introduction to phython programming
Introduction to phython programmingIntroduction to phython programming
Introduction to phython programming
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Introduction to internet
Introduction to internetIntroduction to internet
Introduction to internet
 
Android
AndroidAndroid
Android
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

Learn Ruby Programming in Amc Square Learning

  • 1. Learn RUBY Programming at AMC Square Learning •An interpreted language • a.k.a dynamic, scripting • e.g., Perl •Object Oriented • Single inheritance •High level • Good support for system calls, regex and CGI •Relies heavily on convention for syntax
  • 2. Hello World #!/usr/bin/env ruby puts “Hello world” $ chmod a+x helloWorld.rb $ helloWorld.rb Hello world $ • shell script directive to run ruby • Needed to run any shell script • Call to method puts to write out “Hello world” with CR • Make program executable
  • 3. Basic Ruby •Everything is an object •Variables are not typed •Automatic memory allocation and garbage collection •Comments start with # and go to the end of the line • You have to escape # if you want them elsewhere •Carriage returns mark the end of statements •Methods marked with def … end
  • 4. Control structures If…elsif…else…end case when <condition> then <value>… else… end unless <condition> … end while <condition>… end until <condition>… end #.times (e.g. 5.times()) #.upto(#) (e.g. 3.upto(6)) <collection>.each {block} • elsif keeps blocks at same level • case good for checks on multiple values of same expression; can use ranges grade = case score when 90..100 then “A” when 80..90 then “B” else “C” end • Looping constructs use end (same as class definitions) • Various iterators allow code blocks to be run multiple times
  • 5. Ruby Naming Conventions • Initial characters • Local variables, method parameters, and method names  lowercase letter or underscore • Global variable  $ • Instance variable  @ • Class variable  @@ • Class names, module names, constants  uppercase letter • Multi-word names • Instance variables  separate words with underscores • Class names  use MixedCase • End characters • ? Indicates method that returns true or false to a query • ! Indicates method that modifies the object in place rather than returning a copy (destructive, but usually more efficient)
  • 6. Another Example class Temperature Factor = 5.0/9 def store_C(c) @celsius = c end def store_F(f) @celsius = (f - 32)*Factor end def as_C @celsius end def as_F (@celsius / Factor) + 32 end end # end of class definition Factor is a constant 5.0 makes it a float 4 methods that get/set an instance variable Last evaluated statement is considered the return value
  • 7. Second Try class Temperature Factor = 5.0/9 attr_accessor :c def f=(f) @c = (f - 32) * Factor end def f (@c / Factor) + 32 end def initialize (c) @c = c end end t = Temperature.new(25) puts t.f # 77.0 t.f = 60 # invokes f=() puts t.c # 15.55 attr_accessor creates setter and getter methods automatically for a class variable initialize is the name for a classes’ constructor Don’t worry - you can always override these methods if you need to Calls to methods don’t need () if unambiguous
  • 8. Input and Output - tsv files f = File.open ARGV[0] while ! f.eof? line = f.gets if line =~ /^#/ next elsif line =~ /^s*$/ next else puts line end end f.close ARGV is a special array holding the command- line tokens Gets a line If it’s not a comment or a blank line Print it
  • 9. Processing TSV filesh = Hash.new f = File.open ARGV[0] while ! f.eof? line = f.gets.chomp if line =~ /^#/ next elsif line =~ /^s*$/ next else tokens = line.split /t/ h[tokens[2]] = tokens[1] end end f.close keys = h.keys.sort {|a,b| a <=> b} keys.each {|k| puts "#{k}t#{h[k]}" } Declare a hash table Get lines without n or rn - chomp split lines into fields delimited with tabs Store some data from each field into the hash Sort the keys - sort method takes a block of code as input each creates an iterator in which k is set to a value at each pass #{…} outputs the evaluated expression in the double quoted string
  • 10. Blocks •Allow passing chunks of code in to methods •Receiving method uses “yield” command to call passed code (can call yield multiple times) •Single line blocks enclosed in {} •Multi-line blocks enclosed in do…end •Can use parameters [ 1, 3, 4, 7, 9 ].each {|i| puts i } Keys = h.keys.sort {|a,b| a <=> b }
  • 11. Running system commands require 'find' Find.find('.') do |filename| if filename =~ /.txt$/i url_output = filename.gsub(/.txt$/i, ".html") url = `cat #{filename}`.chomp cmd = "curl #{url} -o #{url_output}"; puts cmd `#{cmd}` end end • require reads in another ruby file - in this case a module called Find • Find returns an array, we create an iterator filename to go thru its instances • We create a new variable to hold a new filename with the same base but different .html extension • We use backticks `` to run a system command and (optionally) save the output into a variable • curl is a command in mac os to retrieve a URL to a file, like wget in unix
  • 12. CGI example require 'cgi' cgi = CGI.new("html3") size = cgi.params.size if size > 0 # processing form in = cgi.params['t'].first.untaint cgi.out { cgi.html { cgi.head cgi.body { "Welcome, #{in}!" } } } else puts <<FORM Content-type: text/html <HTML><BODY><FORM> Enter your name: <INPUT TYPE=TEXT NAME=t><INPUT TYPE=SUBMIT> </FORM></BODY></HTML> FORM end • CGI requires library • Create CGI object • If parameters passed • Process variable t • untaint variables if using them in commands • No parameters? • create form using here document “<<“
  • 13. Reflection ...to examine aspects of the program from within the program itself. #print out all of the objects in our system ObjectSpace.each_object(Class) {|c| puts c} #Get all the methods on an object “Some String”.methods #see if an object responds to a certain method obj.respond_to?(:length) #see if an object is a type obj.kind_of?(Numeric) obj.instance_of?(FixNum)