Created
September 15, 2012 14:50
-
-
Save davybrion/3728341 to your computer and use it in GitHub Desktop.
code snippets for "Using Ruby Classes In C# With IronRuby" post
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 Customer | |
| attr_reader :name | |
| attr_reader :email | |
| def initialize(name, email) | |
| @name = name | |
| @email = email | |
| end | |
| end | |
| class Product | |
| attr_reader :name | |
| attr_reader :price | |
| def initialize(name, price) | |
| @name = name | |
| @price = price | |
| end | |
| end | |
| class OrderItem | |
| attr_reader :product | |
| attr_reader :count | |
| def initialize(product, count) | |
| @product = product | |
| @count = count | |
| end | |
| end | |
| class Order | |
| attr_reader :customer | |
| attr_reader :date | |
| attr_reader :discount | |
| attr_reader :items | |
| def initialize(customer, discount, date) | |
| @customer = customer | |
| @discount = discount | |
| @date = date | |
| @items = [] | |
| end | |
| def add_item(item) | |
| @items << item | |
| 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
| load 'product.rb' | |
| load 'customer.rb' | |
| load 'orderitem.rb' | |
| load 'order.rb' |
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
| var engine = Ruby.CreateEngine(); | |
| engine.ExecuteFile("bootstrap.rb"); | |
| dynamic ruby = engine.Runtime.Globals; | |
| dynamic customer = ruby.Customer.@new("Davy Brion", "[email protected]"); | |
| dynamic product1 = ruby.Product.@new("product1", 50); | |
| dynamic product2 = ruby.Product.@new("product2", 60); | |
| dynamic order = ruby.Order.@new(customer, null, DateTime.Now); | |
| order.add_item(ruby.OrderItem.@new(product1, 5)); | |
| order.add_item(ruby.OrderItem.@new(product2, 5)); | |
| var total = 0; | |
| foreach (dynamic item in order.items) | |
| { | |
| total += item.count * item.product.price; | |
| } | |
| Console.WriteLine("order total: " + total); | |
| Console.WriteLine(); | |
| Console.WriteLine(order.inspect()); |
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
| var engine = Ruby.CreateEngine(); | |
| engine.ExecuteFile("bootstrap.rb"); | |
| dynamic ruby = engine.Runtime.Globals; |
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
| dynamic customer = ruby.Customer.@new("Davy Brion", "[email protected]"); | |
| dynamic product1 = ruby.Product.@new("product1", 50); | |
| dynamic product2 = ruby.Product.@new("product2", 60); | |
| dynamic order = ruby.Order.@new(customer, null, DateTime.Now); |
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
| order.add_item(ruby.OrderItem.@new(product1, 5)); | |
| order.add_item(ruby.OrderItem.@new(product2, 5)); |
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
| var total = 0; | |
| foreach (dynamic item in order.items) | |
| { | |
| total += item.count * item.product.price; | |
| } |
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
| count = item.count | |
| item.count = 5 |
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
| count = item.count() | |
| item.count=(5) # the method name actually is 'count=' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment