Skip to main content

Benx Shen

Benx Shen's Public Library

Ruby Programming/Classes and objects - Wikibooks, collection of open-content textbooks

  •   class Strawberry
    def Strawberry.color
    return "red"
    end

         def self.size
    return "kinda small"
    end

         class << self
    def shape
    return "strawberry-ish"
    end
    end
    end
    Strawberry.color # -> "red"
    Strawberry.size # -> "kinda small"
    Strawberry.shape # -> "strawberry-ish"

    Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass")

Ruby Programming/Data types - Wikibooks, collection of open-content textbooks

  • 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...

Ruby Programming/Ruby basics - Wikibooks, collection of open-content textbooks

  • The function also returns a single value with the return keyword. Technically this isn't necessary -- the value of the last line executed in the function is used as the return value -- but more often than not using return explicitly makes things clearer.
  • Functions interact with blocks through the yield. Every time the function invokes yield control passes to the block. It only comes back to the function when the block finishes. Here's a simple example:


    # Script block2.rb

    def simpleFunction
    yield
    yield
    end

    simpleFunction { puts "Hello!" }

    $ block2.rb
    Hello!
    Hello!
  • 1 more annotations...

Ruby Programming/Introduction to objects - Wikibooks, collection of open-content textbooks

  • In Ruby, methods that end with an exclamation mark (also called a "bang") modify the object. For example, the method upcase! changes the letters of a String to uppercase.


    >> comedian.upcase!
    => "STEPHEN COLBERT"

    Since both of the variables comedian and favorite_comedian point to the same String object, we can see the new, uppercase text using either variable.


    >> comedian
    => "STEPHEN COLBERT"
    >> favorite_comedian
    => "STEPHEN COLBERT"

    Ruby - Introduction to objects - Two variables, modified object.png

Ruby Programming/Alternate quotes - Wikibooks, collection of open-content textbooks

  • in Ruby, there's a better way. You can use the %q operator to apply single-quoting rules, but choose your own delimiter to mark the beginning and end of the string literal.


    puts %q!c:\napolean's documents\tomorrow's bus schedule.txt!
    puts %q/c:\napolean's documents\tomorrow's bus schedule.txt/
    puts %q^c:\napolean's documents\tomorrow's bus schedule.txt^
    puts %q(c:\napolean's documents\tomorrow's bus schedule.txt)
    puts %q{c:\napolean's documents\tomorrow's bus schedule.txt}
    puts %q<c:\napolean's documents\tomorrow's bus schedule.txt>
  • puts %Q!Say "Hello," #{name}.!
    puts %Q/What is "4 plus 5"? Answer: #{4+5}/

Ruby Programming/Hello world - Wikibooks, collection of open-content textbooks

  • The shebang line is read by the shell to determine what program to use to run the script. This line cannot be preceded by any blank lines or any leading spaces. The new hello-world.rb program – with the shebang line – looks like this:


    #!/usr/bin/ruby

    puts 'Hello world'
1 - 20 of 1714 Next › Last »
Showing 20 items per page

Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »

Join Diigo