It's common in Ruby to see some code setup a Struct like this:
class Specialized < Struct.new(:whatever)
# ... define custom methods here...
end| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <title>Ruby Quiz - Dice Roller (#61)</title> | |
| <link rel="stylesheet" type="text/css" href="quiz.css" /> | |
| <link rel="stylesheet" type="text/css" href="print.css" media="print" /> | |
| </head><body> | |
| <div id="page"> | |
| <div id="header"><span class="ruby">Ruby</span> <span class="quiz">Quiz</span></div> |
| module Plugin | |
| def some_method | |
| puts "Plugin" | |
| super | |
| end | |
| end | |
| class A | |
| def some_method | |
| puts "A" |
I've been in multiple discussions lately about the problems with subclassing Ruby's base types.
The main problem is that Ruby sometimes takes shortcuts with the base types, for performance reasons. This can cause them to behave odd. For example, have a look at this subclass:
class MyString < String
def initialize(*)
super
puts "Building MyString: #{inspect}"
end| (set-face-attribute 'window-number-face nil :background "#2a2a2a") | |
| (set-face-attribute 'window-number-face nil :foreground "red") |
| class RequiredArgument | |
| def initialize(one, keyword: false) | |
| p [one, keyword] | |
| end | |
| end | |
| RequiredArgument.new(Hash.new) | |
| # ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0] | |
| # bug.rb:2:in `initialize': wrong number of arguments (0 for 1) (ArgumentError) |
| ~/Desktop [ruby 2.0.0p247]$ cat dont_close_my_logger.rb | |
| require "logger" | |
| log = ARGV.shift or abort "USAGE: #{$PROGRAM_NAME} LOG" | |
| logger = Logger.new(log) | |
| threads = [ ] | |
| threads << Thread.new do | |
| logger.info "Thread one started. Sleeping a bit to make sure thread two is going..." | |
| sleep 1 |
I have a problem that pretty much boils down to this:
a = [1, 2, 3]
b = [1.1, 2.2, 3.3]
# BEGIN GROSS SOLUTION: avert your eyes!
results = [ ]
a.each do |i|
b.each do |j|| headers = nil | |
| File.foreach(path) do |line| | |
| fields = line.split(“|”) | |
| if headers | |
| row = headers.zip(fields) | |
| # … | |
| else | |
| headers = fields | |
| end | |
| end |
| use std::fmt; | |
| use std::os; | |
| struct Stack { | |
| numbers: Vec<f64> | |
| } | |
| impl Stack { | |
| fn new() -> Stack { | |
| Stack{numbers: vec![]} | |
| } |