Skip to content

Instantly share code, notes, and snippets.

@mungruby
mungruby / strcut_new_block.rb
Created October 23, 2011 15:21
Initializing a Struct with a block
describe "Struct", "Struct.new" do
context "initializing with a block" do
it "should support the implementation defined in the block" do
Struct.new("Person", :name, :gender, :age) do
def to_s
"Hi! My name is #{name} and I am a #{age} year old #{gender}."
end
end
fred = Struct::Person.new("Fred", "male", 50)
fred.to_s.should == "Hi! My name is Fred and I am a 50 year old male."
@mungruby
mungruby / 01_splat_unsplat.rb
Created October 26, 2011 15:26
splat and unsplat
v1 = 'a'
v2 = 'b'
v3 = 'c'
def splat(*args)
puts 'splat'
puts args
args
end
@mungruby
mungruby / 01_main.rb
Created October 26, 2011 18:44
top level methods
puts
puts self.inspect # main
puts self.instance_of?(Object) # true
puts "----"
def test_method
"test"
end
@mungruby
mungruby / inject_spec.rb
Created November 2, 2011 14:50
rspec inject examples
C:\projects\ruby\enumerable\inject>rspec inject_spec.rb -fdoc
Enumerable#inject
Array
should respond to #inject
Hash
should respond to #inject
0..100 (a Range)
should respond to #inject
with an explicit subject
@mungruby
mungruby / display_superheroes_block
Created November 5, 2011 01:28
A display_superheroes method that accepts an alternative block
Superhero = Struct.new :name, :origin, :nemesis, :nick_name
SuperHeroes = [
Superhero.new("Batman", "Gotham City", "Joker", "Caped Crusader"),
Superhero.new("Robin", "Gotham City", "Joker", "Boy Wonder"),
Superhero.new("Superman", "Krypton", "Lex Luthor", "Kal El"),
Superhero.new("Supergirl", "Krypton", "Bizzaro", "Kara Zor-El") ]
def display_superheroes *superheroes, &block
class SomeClassVariables
@@class_level_var = "class_level_var"
def self.add_class_var
@@class_method_var = "class_method_var"
end
def self.show_class_var
@mungruby
mungruby / class_new_001.rb
Created April 14, 2012 02:06
Metaprogramming 001
attributes = %w[name species]
MyClass = Class.new do
puts attributes
attr_accessor *attributes
define_method :initialize do |data|
puts attributes
puts data
attributes.each_with_index do |attribute, idx|
puts "#{idx} : #{attribute} : #{data[idx]}"
self.__send__ "#{attribute}=", data[idx]
@mungruby
mungruby / class_new_002.rb
Created April 14, 2012 02:39
Metaprogramming 002
attributes = %w[name species]
MyReadOnlyClass = Class.new do
attr_reader *attributes
define_method :initialize do |data|
attributes.each_with_index do |attribute, idx|
self.instance_variable_set "@#{attribute}", data[idx]
end
end
end
@mungruby
mungruby / var_args.rb
Created April 21, 2012 01:51
Variable Argument Lists
class VariableArguments
def initialize *args
@first, @second, @third = *args
end
end
v = VariableArguments.new
puts v.inspect
v = VariableArguments.new("Fred")
@mungruby
mungruby / 003_Struct.rb
Created April 28, 2012 02:08
Add a method to a Struct
members = %w[NAME ADDRESS]
members.map!(&:downcase)
members.map!(&:to_sym)
Customer = Struct.new(*members) do
def print
puts self.to_a.join(",")
end
end