“ Housekeeping ” Welcome to today ’ s ACM Webinar. The presentation starts at the top of the hour. • • If you are experiencing any problems/ issues, refresh your console by pressing the F5 key on your keyboard in W indow s , Com m and + R if on a Mac , or refresh your browser if you ’ re on a mobile device; or close and re-launch the presentation. You can also view the Webcast Help Guide, by clicking on the “ Help ” widget in the bottom dock. • To control volume, adjust the master volume on your computer. • If you think of a question during the presentation, please type it into the Q&A box and click on the submit button. You do not need to wait until the end of the presentation to begin submitting questions. At the end of the presentation, you ’ ll see a survey URL on the final slide. Please take a minute to • click on the link and fill it out to help us improve your next webinar experience. • You can download a copy of these slides by clicking on the Resources widget in the bottom dock. • This presentation is being recorded and will be available for on-demand viewing in the next 1-2 days. You will receive an autom atic e-m ail notification when the recording is ready. 1
Ruby for the Nuby ACM Learning Webinar David A. Black Lead Developer Cyrus Innovation March 27, 2014
ACM Learning Center http: / / learning.acm.org 1,400+ trusted technical books and videos by leading publishers including O ’ Reilly, • Morgan Kaufmann, others • Online courses with assessments and certification-track mentoring, member discounts on tuition at partner institutions • Learning Webinars on big topics (Cloud/ Mobile Development, Cybersecurity, Big Data, Recommender Systems, SaaS, Agile, Machine Learning, Natural Language Processing, Parallel Programming, etc.) • ACM Tech Packs on top current computing topics: Annotated Bibliographies compiled by subject experts • Popular video tutorials/ keynotes from ACM Digital Library, A.M. Turing Centenary talks/ panels • Podcasts with industry leaders/ award winners 3
“ Housekeeping ” Welcome to today ’ s ACM Webinar. The presentation starts at the top of the hour. • • If you are experiencing any problems/ issues, refresh your console by pressing the F5 key on your keyboard in W indow s , Com m and + R if on a Mac , or refresh your browser if you ’ re on a mobile device; or close and re-launch the presentation. You can also view the Webcast Help Guide, by clicking on the “ Help ” widget in the bottom dock. • To control volume, adjust the master volume on your computer. • If you think of a question during the presentation, please type it into the Q&A box and click on the submit button. You do not need to wait until the end of the presentation to begin submitting questions. At the end of the presentation, you ’ ll see a survey URL on the final slide. Please take a minute to • click on the link and fill it out to help us improve your next webinar experience. • You can download a copy of these slides by clicking on the Resources widget in the bottom dock. • This presentation is being recorded and will be available for on-demand viewing in the next 1-2 days. You will receive an autom atic e-m ail notification when the recording is ready. 4
Talk Back • Use the Facebook widget in the bottom panel to share this presentation with friends and colleagues • Use Twitter widget to Tweet your favorite quotes from today ’ s presentation with hashtag # ACMWebinarRuby • Submit questions and comments via Twitter to @acmeducation – we ’ re reading them!
About me • Lead developer at Cyrus Innovation • Rubyist since 2000 • Author of The Well-Grounded Rubyist (Manning 2009; second edition forthcoming 2014) • Founding former director of Ruby Central • Long-time RubyConf organizer • Ruby/ Ruby on Rails trainer • Ruby standard library contributor (chief author of scanf.rb)
About Ruby • Created and still guided by Yukihiro "matz" Matsumoto • First announced in February 1993 • Version 1.0 12/ 25/ 1996 • Object-oriented • Ancestors include Smalltalk, LISP, Perl, CLU • Very dynamic • Untyped variables • Introduced widely outside Japan via Programming Ruby (Pragmatic Programmers, 2000) • Further popularized by Ruby on Rails (2004)
Some basic basics puts "Hello, world!" x = 10 y = x * 2 def greet puts "Hello, world!" end def shout puts "Hello, world!".upcase end greet => Hello, world! shout => HELLO, WORLD!
Some basic basics, cont'd a = 1 b = 2 if a > b puts "Huh?" else puts "That's more like it." end => That's more like it.
REPL with irb • Command-line interactive Ruby interpreter • Ships with Ruby $ irb --simple-prompt >> a = 1 => 1 >> b = 2 => 2 >> a + b => 3 >> "David".upcase => "DAVID"
Ruby’s object model • (Almost) Everything is an object • including classes • Objects are instances of classes • but are “teachable” individually • The class hierarchy descends from Object and BasicObject • BasicObject is barebones • Object is equipped with a good handful of methods (functionality)
Instantiating an object object = Object.new puts object.object_id => 2156388440
Sending messages to objects • AKA calling methods on objects – methods get called when a message is understood by the object • Dot notation string = "Sample string" # A String object puts string.upcase => SAMPLE STRING puts string.reverse => gnirts elpmaS
“Teaching” an object • Individual objects can be "taught" singleton methods • Singleton methods are callable only on the one object – not on other objects of that object's class object = Object.new def object.talk puts "Good afternoon from an object" end object.talk => Good afternoon from an object
Writing your own classes class Person def talk puts "Good afternoon from a Person" end end david = Person.new david.talk => Good afternoon from a Person
The initialize method class Person def initialize(name) # Save the incoming name in an instance variable @name = name end def talk # Reuse the instance variable later puts "Good afternoon from #{@name}." end end david = Person.new("David") david.talk => Good afternoon from David
Class methods • A cousin of "static" methods in other languages class Person def Person.planet "Earth" end end puts "People live on #{Person.planet}." => People live on Earth.
Inheritance • Ruby supports single inheritance only – (More complex modeling available via modules) class Animal def planet "Earth" end end class Human < Animal end h = Human.new puts h.planet => Earth
Modules • Like classes, but don't have instances • Can be "mixed in" to classes – mix-ins add functionality – instances of the class can use methods from the mix-ins
Module example module Vocal def talk puts "Greetings" end end class Person include Vocal # Mix in the Vocal module end david = Person.new david.talk => Greetings
Variables Local: a = 1 Instance: @a = 1 Global: $a = 1 Class: @@a = 1
Local variables • Scoped to a class definition, module definition, or method definition • Outside of the above, they function as "top-level" variables
Three separately-scoped a variables a = "top-level variable a" def my_method a = 2 end class MyClass a = 3 end puts a => top-level variable a
Instance variables • For saving state in an object class Person def initialize(name) @name = name end def talk puts "Good afternoon from #{@name}." end end david = Person.new("David") david.talk => Good afternoon from David
Global variables • For the most part, don't create them • Some handy built-in ones – $: the library load path – $/ the input record separator – $$ id of current process – $? the result of most recent system command call – $1, $2, $3… . parenthetical captures from most recent regular expression match
Class variables • Scoped per class hierarchy • Shared by classes and their instances class Human @@planet = "Earth" # Used at class-level def planet @@planet # Used at instance-level end end
Constants • Start with capital letter • Used for names of classes and modules • Also defined inside classes and modules • Not totally constant… – Can be redefined (but you get a warning) Resolved with :: operator • class Person PLANET = "Earth" end puts Person::PLANET => Earth
Booleans and nil • true and false are objects • nil is an object • Every object has a boolean value – the boolean value of false and nil is false – the boolean value of everything else is true if 0 puts "Zero is true in Ruby!" end => Zero is true in Ruby!
Recommend
More recommend