十一月 12, 2009
十一月 11, 2009
十一月 10, 2009
Diigo Diary 11/10/2009
Posted from Diigo. The rest of my favorite links are here.
十一月 7, 2009
Diigo Diary 11/07/2009
Posted from Diigo. The rest of my favorite links are here.
十一月 5, 2009
十一月 4, 2009
Diigo Diary 11/04/2009
Posted from Diigo. The rest of my favorite links are here.
十一月 3, 2009
Diigo Diary 11/03/2009
Posted from Diigo. The rest of my favorite links are here.
十月 30, 2009
Diigo Diary 10/30/2009
Posted from Diigo. The rest of my favorite links are here.
十月 29, 2009
Diigo Diary 10/29/2009
Posted from Diigo. The rest of my favorite links are here.
十月 25, 2009
Diigo Diary 10/25/2009
-
Ruby Exceptions: Ruby Study Notes – Best Ruby Guide, Ruby Tutorial
-
Ruby Programming/Classes and objects – Wikibooks, collection of open-content textbooks
-
class Strawberry def Strawberry.color return "red" enddef self.size return "kinda small" endclass << 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.classSee? 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.
-
-
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!
-
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" end animals { |x| puts "Hello, #{x}" }$ block1.rb Hello, Tiger Hello, Giraffe
-
-
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
comedianandfavorite_comedianpoint to the same String object, we can see the new, uppercase text using either variable.>> comedian => "STEPHEN COLBERT" >> favorite_comedian => "STEPHEN COLBERT"
-
-
Ruby Programming/Alternate quotes – Wikibooks, collection of open-content textbooks
-
in Ruby, there’s a better way. You can use the
%qoperator 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.rbprogram – with the shebang line – looks like this:#!/usr/bin/ruby puts 'Hello world'
-
Posted from Diigo. The rest of my favorite links are here.
