Benx Blog

十月 25, 2009

Diigo Diary 10/25/2009

Filed under: Diigo Diary — benxshen @ 8:30 上午
  • tags: ruby, exception, intro

  • tags: no_tag

    •   class Strawberry
           def Strawberry.color
             return "red"
           def self.size
             return "kinda small"
           class << self
             def shape
               return "strawberry-ish"
         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")

  • tags: no_tag

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

  • tags: no_tag

    • 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
      simpleFunction { puts "Hello!" }
      $ block2.rb
    • The simpleFunction simply yields to the block twice — so the block is run twice and we get two times the output. Here’s an example where the function passes a parameter to the block:

      # Script block1.rb
      def animals
        yield "Tiger"
        yield "Giraffe"
      animals { |x| puts "Hello, #{x}" }
      $ block1.rb
      Hello, Tiger
      Hello, Giraffe
  • tags: no_tag

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

      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
      >> favorite_comedian
      Ruby - Introduction to objects - Two variables, modified object.png
  • tags: no_tag

    • 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}/
  • tags: no_tag

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

      puts 'Hello world'
  • tags: outlook, tip, howto, *i_use_it*, cool, GTD

Posted from Diigo. The rest of my favorite links are here.

發表迴響 »


RSS feed for comments on this post. TrackBack URI


在下方填入你的資料或按右方圖示以社群網站登入: Logo

您的留言將使用 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )


連結到 %s

在 建立免費網站或網誌.

%d 位部落客按了讚: