Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 14:50
Show Gist options
  • Select an option

  • Save davybrion/3728341 to your computer and use it in GitHub Desktop.

Select an option

Save davybrion/3728341 to your computer and use it in GitHub Desktop.
code snippets for "Using Ruby Classes In C# With IronRuby" post
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
load 'product.rb'
load 'customer.rb'
load 'orderitem.rb'
load 'order.rb'
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());
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;
}
count = item.count
item.count = 5
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