Benx Shen's Library tagged → View Popular
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'
.NET Code Camp 2006 - 10 Things - #5: Don't Worry About Interfaces
-
- If it walks like a duck,
- And talks like a duck,
- Then we can treat it like a duck.
- (who cares what it really is)
- Then we can treat it like a duck.
Ruby Uses Duck Typing
- If it walks like a duck,
Functional Programming HOWTO — Python v3.1.1 documentation
-
Generators are a special class of functions that simplify the task of writing
iterators. Regular functions compute a value and return it, but generators
return an iterator that returns a stream of values. -
When you call a generator function, it doesn’t return a single value; instead it
returns a generator object that supports the iterator protocol. On executing
the yield expression, the generator outputs the value of i, similar to a
return statement. The big difference between yield and a return
statement is that on reaching a yield the generator’s state of execution is
suspended and local variables are preserved. On the next call to the
generator’s .__next__() method, the function will resume executing. - 4 more annotations...
8.3. collections — Container datatypes
-
8.3. collections — Container datatypes
-
8.3.4. defaultdict objects
- 2 more annotations...
5. Built-in Types
-
5.6.2. Old String Formatting Operations
-
The objects returned by dict.keys(), dict.values() and
dict.items() are view objects. They provide a dynamic view on the
dictionary’s entries, which means that when the dictionary changes, the view
reflects these changes.
1. The way of the program — How to Think Like a Computer Scientist: Learning with Python v2nd Edition documentation
-
A program is a sequence of instructions that specifies how to perform a
computation.
VOA News - American History Series: Lincoln Needs a Victory
-
"My chief object in this struggle is to save the
Union. It is not either to save or destroy slavery. If I could save the Union
without freeing any slave, I would do it. And if I could save it by freeing all
the slaves, I would do it. And if I could save it by freeing some and leaving
others alone, I would also do that."What I do about slavery, and the colored race, I
do because I believe it helps to save the Union. This is how I see my official
duty. It does not change my wish -- as a person -- that all men everywhere
could be free."
Selected Tags
Sponsored Links
Top Contributors
Groups interested in no_tag
-
Web 2.0 Tools
Items: 10 | Visits: 883
Created by: Claire Miller
-
Erotica
Items: 40 | Visits: 3362
Created by: Ainis
-
Digital Citizenship/Cyberbullying Video Clips
Items: 27 | Visits: 2023
Created by: Anne Bubnic
Diigo is about better ways to research, share and collaborate on information. Learn more »
Join Diigo
