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?
- 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
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'
Sponsored Links
Top Tags
- 137javascript,
- 135java,
- 113*精華*,
- 110tutorial,
- 103programming,
- 100article,
- 96cool,
- 81tool,
- 79resources,
- 79jquery-plugin,
View All Recent Tags (49)
- 10javascript,
- 9Python,
- 9read_it_later,
- 6tips,
- 6java,
- 4*精華*,
- 4intro,
- 4performance,
- 4blog,
- 4code,
- 4*i_learning*,
- 3vim,
- 3programming,
- 3howto,
- 3tutorial,
- 3education,
- 3css,
- 3article,
- 3scala,
- 3tools,
- 3framework,
- 2ubuntu,
- 2travel,
- 2science,
- 2plugin,
- 2eclipse,
- 2sample,
- 2english,
- 2study,
- 2config,
- 2toolkit,
- 2download,
- 2groovy,
- 2learning,
- 2jquery,
- 2reference,
- 2html,
- 2library,
- 2eclipse-plugin,
- 2jquery-plugin,
- 1theme,
- 1history,
- 1*經典*,
- 1作業系統,
- 1component,
- 1util,
- 1taiwan,
- 1cookbook,
- 1exception
Public Tags (1050)
Benx Shen's Public Lists (0)
No lists have been created yet.
"List" is a great way to organize, share and display your specific collection of bookmarks.
Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »
Join Diigo