This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| v1 = 'a' | |
| v2 = 'b' | |
| v3 = 'c' | |
| def splat(*args) | |
| puts 'splat' | |
| puts args | |
| args | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| puts | |
| puts self.inspect # main | |
| puts self.instance_of?(Object) # true | |
| puts "----" | |
| def test_method | |
| "test" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class VariableArguments | |
| def initialize *args | |
| @first, @second, @third = *args | |
| end | |
| end | |
| v = VariableArguments.new | |
| puts v.inspect | |
| v = VariableArguments.new("Fred") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
OlderNewer