Skip to main content

Diigo Home

Ruby Programming/Data types - Wikibooks, collection of open-content textbooks - The Diigo Meta page

en.wikibooks.org/...Data_types - Cached - Annotated View

Benx Shen's personal annotations on this page

benxshen
Benxshen bookmarked on 2009-10-24
  • h = {"hash?" => "yep, it's a hash!", "the answer to everything" => 42, :linux => "fun for coders."}
    puts "Stringy string McString!".class
    puts 1.class
    puts nil.class
    puts h.class
    puts :symbol.class

    See? Everything is an object.

  • If I kept doing this, array2 wouldn't hold any elements. I can check for this condition by calling the empty? method. For example, the following bit of code moves all the elements from one array to another:


    array1 << array2.pop until array2.empty?
  • Time for the last three:


    string1 = 451.to_s
    string2 = 98.6.to_s
    int = 4.5.to_i
    float = 5.to_f

    to_s converts floats and integers to strings. to_i converts floats to integers. to_f converts integers to floats. There you have it. All the data types of Ruby in a nutshell.

This link has been bookmarked by 1 people . It was first bookmarked on 24 Oct 2009, by Benx Shen.

  • 24 Oct 09
    • h = {"hash?" => "yep, it's a hash!", "the answer to everything" => 42, :linux => "fun for coders."}
      puts "Stringy string McString!".class
      puts 1.class
      puts nil.class
      puts h.class
      puts :symbol.class

      See? Everything is an object.

    • If I kept doing this, array2 wouldn't hold any elements. I can check for this condition by calling the empty? method. For example, the following bit of code moves all the elements from one array to another:


      array1 << array2.pop until array2.empty?
    • 1 more annotations...