1 / 21

Ruby in 20 minutes

Ruby in 20 minutes. John Pinney j.pinney@imperial.ac.uk. What is Ruby?. A true object-oriented language with easily understandable syntax and convenient notations. => pure / elegant / powerful

heman
Télécharger la présentation

Ruby in 20 minutes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rubyin 20 minutes John Pinney j.pinney@imperial.ac.uk

  2. What is Ruby? A true object-oriented language with easily understandable syntax and convenient notations. => pure / elegant / powerful “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.” Yukihiro Matsumoto (“Matz”)

  3. The Ruby interpreter > irb puts "Hello World" Hello World

  4. (Almost) everything is an object There are no primitives in Ruby. Nearly everything can be considered as an object, i.e. is an instance of a class has an interface for method invocation

  5. "hello".reverse => "olleh” 44.even? => true nil.class => NilClass nil.class.class => Class

  6. Parts of speech Variables Numbers Strings Symbols Constants Ranges Regular Expressions • Methods + arguments • Blocks + arguments • Arrays • Hashes • Operators • Keywords

  7. Variables local_variable = 3.0 @instance_variable = 4.6 @@class_variable = 8.9 (@ = “attribute”) $global_variable = 2.5

  8. Numbers 1.class => Fixnum 1.0.class => Float

  9. Symbols Symbols (starting with a colon ) are like lightweight strings. They always point to the same object, wherever used in the code. x = :my_symbol

  10. Methods front_door.open front_door.open.close front_door.is_open? front_door.paint(3,:red) front_door.paint(3,:red).dry(30).close

  11. Kernel methods are invoked by their names alone. This is just a bit of syntactic sugar. print"Hello\n" Hello Kernel.print("Hello\n") Hello (print is really a class method of Kernel)

  12. Operators Operators behave as you would expect: 1 + 2 3 But in Ruby they are actually methods! 1.+(2) 3 Since all operators are methods, they can be defined as you like for your own classes, or even re-defined for existing classes.

  13. Blocks Any code surrounded by curly braces is a closure, known as a block: 20.times{ print 'CAG' } CAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAG Blocks are sets of instructions that can be passed around the program.

  14. Blocks can also take arguments, delimited by pipe characters: "Hello".each_char{|ch| puts ch} H e l l o

  15. Braces can be replaced by the keywords do and end. "Hello".each_chardo |ch| puts ch end H e l l o

  16. Effective use of blocks can allow highly transparent code: a = [ "a", "b", "c", "d" ] a.collect{ |x| x + "!" } ["a!", "b!", "c!", "d!"]

  17. Regular expressions Regex is very simple to use in Ruby (much nicer than Python!) text = "Cats are smarter than dogs”; if ( text =~ /(C|c)at(.*)/ ) then puts "I found a cat" end I found a cat

  18. Example class Greeter def initialize(name = "World") @name = name end defsay_hi puts "Hi #{@name}!" end defsay_bye puts "Bye #{@name}, come back soon." end end

  19. g = Greeter.new("Pat") g.say_hi Hi Pat! g.say_bye Bye Pat, come back soon. g.name NoMethodError: undefined method 'name' for #<Greeter:0x007f91b18708f8 @name="Pat">

  20. class Greeter attr_accessor :name end g.name => "Pat" g.name = "John" g.say_hi Hi John!

  21. Sources: https://www.ruby-lang.org/en/documentation/quickstart/ http://docs.ruby-doc.com/docs/ProgrammingRuby/ http://mislav.uniqpath.com/poignant-guide/ http://tryruby.org/

More Related