Created
February 7, 2010 23:34
-
-
Save mariochavez/297759 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
public class Bowling | |
{ | |
public Bowling() | |
{ | |
} | |
public void Roll(int pins) | |
{ | |
throw new NotImplemented(); | |
} | |
public int Score() | |
{ | |
throw new NotImplemented(); | |
} | |
} |
This file contains 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 Mensajes | |
def initialize(nombre = "Mundo") | |
@nombre = nombre | |
end | |
def hola | |
puts "Hola #{@nombre}" | |
end | |
def adios | |
puts "Adios #{@nombre}" | |
end | |
end | |
m = Mensajes.new "Mario" | |
m.hola | |
m.adios | |
# Inspeccion de metodos y llamadas dinamicas | |
Mensajes.instance_methods | |
Mensajes.instance_methods(false) | |
# Refleccion | |
m.methods | |
m.respond_to?("hola") | |
m.respond_to?("holas") | |
m.respond_to?("to_s") | |
m.send("hola") | |
This file contains 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 Mensajes | |
attr_accessor :nombres | |
def initialize(nombres = "Mundo") | |
@nombres = nombres | |
end | |
def hola | |
if @nombres.nil? | |
puts "..." | |
elsif @nombres.respond_to?("each") | |
@nombres.each do |nombre| | |
puts "Hola #{nombre}" | |
end | |
else | |
puts "Hola #{@nombres}" | |
end | |
end | |
def adios | |
if @nombres.nil? | |
puts "..." | |
elsif @nombres.respond_to?("join") | |
puts "Adios #{@nombres.join(", ")}. Vuelvan pronto." | |
else | |
puts "Adios #{@nombres}. Vuelve pronto." | |
end | |
end | |
end | |
m = Mensajes.new "Mario" | |
m.hola | |
m.nombres = ["Mario", "Alberto"] | |
m.hola | |
# Esto genera un error ya que el metodo size no existe | |
m.size | |
class Mensajes | |
private | |
def method_missing(method, *args, &block) | |
puts "Metodo no encontrado #{method}" | |
end | |
end | |
# Ya no genera el error, nuestra clase dinamicamente lo "atrapa" | |
m.size | |
class Mensajes | |
private | |
def method_missing(method, *args, &block) | |
@nombres.send(method, *args, &block) | |
end | |
end | |
# Ahora size funciona de menare diferente si el valor de nombres es un string o un arreglo | |
m.nombre="Mario" | |
m.size | |
m.nombres=["Mario", "Alberto"] | |
m.size | |
This file contains 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
def hola | |
puts "Hola mundo" | |
end | |
puts hola | |
def hola(nombre) | |
puts "Hola #{nombre}" | |
end | |
puts hola "Mario" | |
def hola(nombre = "Mundo") | |
puts "Hola #{nombre}" | |
end | |
puts hola | |
puts hola "Mario" | |
def hola(nombre = "Mundo") | |
puts "Hola #{nombre.reverse}" | |
end | |
puts hola "Mario" |
This file contains 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
# Ejecutar | |
irails blog | |
# Una vez creada la aplicacion hay que modificar los siguientes archivos dentro del folder blog | |
# config/environment.rb y agregar estas lineas al inicio | |
require 'rubygems' | |
require 'ironruby_sqlserver' | |
# en el archivo config/database.yml modificamos la definicion de nuestra conexion a la base | |
# de datos dev para que quede de la siguiente forma, | |
# hay que modifcar el nombre de host a nuestro nombre de servidor de SQL | |
# y la base de datos ironruby_on_rails_dev debe existir en el servidor: | |
development: | |
mode: ADONET | |
adapter: sqlserver | |
host: (local) | |
database: ironruby_on_rails_dev | |
integrated_security: true | |
# Dentro del folder blog ejecutamos los siguientes comandos | |
ir script\generate rspec_scaffold post titulo:string cuerpo:text publicado:boolean | |
irake db:migrate | |
ir script\server |
This file contains 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
# http://blogs.microsoft.co.il/blogs/shayf/archive/2009/10/26/C_2D00_Recorder_2D00_using_2D00_IronRuby.aspx | |
class Recorder | |
# Initialize an array that will save the calls | |
def initialize | |
@calls = [] | |
end | |
# Save the calls to method_missing | |
def method_missing(name, *args, &block) | |
@calls << [name, args, block] | |
end | |
# Playback the calls on a given object | |
def playback(obj) | |
@calls.each do |name, args, block| | |
obj.send name, *args, &block | |
end | |
end | |
end | |
# Record calls | |
rec = Recorder.new | |
rec.reverse! | |
rec.insert 2, "ABAB" | |
rec.delete! "A" | |
# Playback them on a real object | |
str = "Hello World" | |
puts str # Prints "dlBBroW olleH" |
This file contains 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
n = ["Mario", "Alberto", "Chavez"] | |
n.grep /r/ | |
"Mensaje de prueba para mostrar Ruby".gsub /\s/,"_" |
This file contains 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
# Patch http://ironruby.net/Documentation/Real_Ruby_Applications/Sinatra | |
require 'rubygems' | |
require 'sinatra' | |
get '/' do | |
"Hello World!" | |
end | |
get '/acercade' do | |
"Aplicacion de IronRuby" | |
end | |
get '/:nombre' do |nombre| | |
"Hola #{nombre}!|" | |
end |
This file contains 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
3.times { puts "Hola" } | |
["Mario", "Alberto"].each {|n| puts n} | |
["Mario", "Alberto"].each do |n| | |
s = n.reverse | |
puts s | |
end | |
n = ["Mario", "Alberto", "Chavez"] | |
n.sort | |
n.sort! | |
n.delete "Alberto" | |
n = ["Mario", "Alberto", "Chavez"] | |
n.include?("Chavez") | |
puts "Si lo tiene" if n.include?("Chavez") | |
puts "Si lo tiene" unless n.include?("Chavez") | |
n.delete "Chavez" | |
puts "Si lo tiene" if n.include?("Chavez") | |
puts "Si lo tiene" unless n.include?("Chavez") |
This file contains 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
"Hola Ironruby" | |
m = "Hola Ironruby" | |
puts m | |
m = 5 | |
m + 1 | |
Math.sqrt(4) | |
Math.sqrt 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment