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

Ruby Basics and More!

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Ruby Basics and More!

Visual Aid for the Second Ruby Hangout by Kodermine

Florida Ivanne Elago

March 08, 2014

Transcript

21 slides
  1. This is a continuation of the first Ruby hangout that

    was unfortunately not recorded. However, show notes are available http://bit.ly/Pdc5kK
  2. • User input • Methods ◦ More About Ruby Methods

    ◦ Optional Parameters ◦ Named Parameters ◦ Splat Parameters • Symbols • Hashes ◦ Hash methods What we’re gonna learn today
  3. Getting user input: name = gets.chomp gets - accepts single

    line of data from stdin(keyboard) and returns a string. chomp - removes extra whitespace from the returned string. User input
  4. Symbols A symbol in Ruby is similar to a string,

    the difference is that a string is mutable and a symbol is immutable. Meaning only one copy of a symbol is gonna be created through the duration of the program’s execution. foo = “hello world” bar = “hello world” foz = :hello_world baz = :hello_world # let’s check the object id’s of this so we know what’s going on.
  5. Methods A method is a set of expressions/lines of code

    that returns a value. Other languages refer to methods as “functions”
  6. def say_hello(name) puts “hello #{name}” end def - defines the

    method say_hello - name of the the method name - parameter puts “hello #{name}” - expression inside function that prints out hello and the name from the parameter. end - closes the method definition Methods - Syntax
  7. Optional parameters are useful if we want to assign default

    values to our parameters. def say_hello(name=”world”, age=””) puts “hello #{name}” end # Calling the method say_hello(“Florida”, 16) Optional parameters
  8. Named parameters Ruby 2.0 added support for named parameters, before

    Ruby 2.0 people usually make use of the “options” parameters. def say_hello(first_name: “Foo”, last_name: “Bar, location: nil) puts “#{first_name} #{last_name}” puts “#{location} is a cool place” if location end def say_hello(options = {}) puts “#{options[:first_name]} #{ options[:last_name]}” puts “#{options[:location]}” end
  9. Splat arguments Splat arguments capture multiple arguments. After passing the

    arguments it will be turned into an array. def list_of_cool_friends(*friends) friends.each { |friend| puts “#{friend} you are cool!” } end
  10. Hash A Hash is a dictionary-like collection of unique keys

    and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. - Ruby Docs spanish = { hello: “hola”, cool: “fresco” }
  11. Hash Methods - delete takes an argument, and deletes pair

    with that key. spanish.delete(:hello) => “hola” This will also return the value of the deleted key-value pair. Passing an optional block - will be called if key doesn’t exist spanish.delete(:hello) { |key| puts “#{key} doesn’t exist” }
  12. Hash methods - has_key?, has_value, include? has_key/has_value/include? will return a

    true or false value, if the key or the value exists in the hash spanish.has_key?(:hello) => true spanish.has_key?('hola') => false spanish.has_value?('hola') => true spanish.include?(:cool) => true
  13. Hash methods - to_s Returns a string equivalent of our

    hash spanish.to_s => "{:hello=>\"hola\", :cool=>\"fresco\"}"
  14. Hash - default Sets a default value to return just

    in case the key doesn’t exist spanish.default = “Hola” spanish[:candy] => “Hola”
  15. Hash - each goes through each of the hash, we

    pass it a block that receives two arguments (the key and the value). spanish.each {|key, value| puts "#{key.to_s} is #{value} in spanish"} hello is hola in spanish cool is fresco in spanish => {:hello=>"hola", :cool=>"fresco"}
  16. Hash - each_key goes through each key, we pass it

    a block and it receives the “key” argument puts "List of words" spanish.each_key {|key| puts "#{key.to_s}"} hello cool => {:hello=>"hola", :cool=>"fresco"}
  17. Hash - each_value Has the same syntax as each_key but

    the block receives the values puts "List spanish of words" spanish.each_value {|value| puts "#{value}"} hola fresco => {:hello=>"hola", :cool=>"fresco"}

More Decks by Florida Ivanne Elago

Other Decks in Programming