Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing Ruby

Introducing Ruby

An introduction to the Ruby Language.

Avatar for Benjamin Reed

Benjamin Reed

April 19, 2013

More Decks by Benjamin Reed

Other Decks in Technology

Transcript

  1. •  Like most interpreted languages, Ruby mainly runs in a

    Shell. •  There are projects to create a GUI for Ruby (such as Shoes); however, the command line tools continue to dominate. •  Ruby Programs can be easily installed using RubyGems. •  You can install multiple versions of Ruby on your computer with RVM and rbenv.
  2. •  Use puts for addition of newline constant and print

    for no newline constant. •  Actually nobody writes it like this! •  Ruby does not require parenthesis for methods •  No semicolons after each statement. puts(“Hello World”) print(“Hello World\n”)
  3. puts “Hello World” print “Hello World\n” •  Use puts for

    addition of newline constant and print for no newline constant. •  Actually nobody writes it like this! •  Ruby does not require parenthesis for methods •  No semicolons after each statement.
  4. •  Don’t Express the Data Types! •  Write it like

    you would in Math. •  To initialize objects use the new method. •  Not normally a good idea to override new; however some projects do. x = 4 today = Time.now
  5. •  No pre x implies a regular variable •  A

    dollar sign ($) implies global accessibility •  An ampersand (@) implies an instance variable •  2 ampersands (@@) implies a static class variable me = ‘ben’ # available to this area $you = tu # available to the entire program @age = 17 # available to the instance methods of class @@version = 1 # available statically to the class
  6. •  Just like most other languages. •  Only use parenthesis

    when needed. •  Or you can save space and pretty it up! •  Yes! Ruby let’s you right it on 1 line. if a == b c = a end c = a if a == b
  7. •  Use elsif to add another if statement •  Else

    is also provided if a == b c = a elsif a < b c = b - a end if a == b c = a elsif a < b c = b – a else c = b end
  8. •  Methods in Ruby are very exible. •  Unfortunately, methods

    cannot be overloaded. •  You can de ne optional parameters. •  Ruby 2.0 announced named parameters. •  Can be overridden in child classes. •  May have multiple aliases. •  Parenthesis are optional – recommended for some operations •  Called on objects using a (.) dot
  9. •  Use def to de ne the start of a

    method •  Use end to indicate the end •  Don’t use brackets! def age return 17 end
  10. •  No point in writing return 99% of the time.

    •  The last value in a method is returned. •  If you need to return before the last line, a return keyword is provided. def age 17 # returns 17 end
  11. •  Simply name a parameter or list multiple with commas

    to provide a list. •  To make a parameter optional, specify a value to use if none is speci ed during invocation. •  This makes from optional (better than overloading). def go(to, from) end def go(to, from = “here”) end
  12. •  Ruby 2.0 was just released in 2013. •  Named

    parameters allow users to specify names for the values, and this cleans up the code. •  To call this method, we would write: •  Coke is implied because named parameters are optional parameters (this means that the order does not matter)! def eat_dinner(food: “turkey”, drink: “coke”) end eat_dinner food: “spaghetti”
  13. •  Classes use the keyword class •  Use < to

    indicate inheritance class Mammal # parent class end class Human < Mammal # child class end