Quantcast
Channel: Tech Adventures
Viewing all articles
Browse latest Browse all 9

Quick Ruby Reference for .Net Developer

$
0
0

I use C# at my day job and Ruby for other work. After years of thinking and programing in C#, lots of things comes naturally while programming in C#. In the evenings, when I switch to Ruby, many times I find myself thinking about how to do simple thing like checking type in Ruby. I have put together a list of a handy reference for Ruby common constructs, if you are also switching between languages, you might find it useful. I have covered 12 common things here, more in future posts.

1) Static Methods

class DoorFactorydef self.make_door width,height"Door (size #{width} by #{height})" end end puts DoorFactory.make_door(3,8)

2) Hash - Common Operations

door_prices = { "small"=>34.00, "medium"=>46.00, "large"=>55.00} puts door_prices.select{|k| k =="medium"} # {"medium"=>46.0} puts door_prices.select{|k,v| v >35} # {"medium"=>46.0, "large"=>55.0} puts door_prices.has_key?("super-large") # false puts door_prices.has_value?(46.00) # true

3) Array – Common Operations

door_sizes = ["small","medium","large"] puts door_sizes.find_index("super-large").nil? # true door_sizes.each_index{|i| puts door_sizes[i]} # small medium large puts door_sizes.include?("super-large") # false puts door_sizes.empty? # false

4) Iteration

door_prices.each{|k,v| puts "#{k} = #{v}"} door_sizes.each{|s| puts s}

5) Type Check

price =56.00 puts price.class # Float puts price.is_a? Integer # false puts price.instance_of? Float # true

6) Method Exists?

puts DoorFactory.respond_to?'make_door' # true because it's static method agile_door_factory = DoorFactory.new puts agile_door_factory.respond_to?'make_door' # false because it's not an instance method

7) Try, Catch, Finally

begin3/0 rescue puts 'There was an exception for sure...' ensure puts 'Exception or no-exception, I am gonna be there...' end

8) Properties

class Door attr_reader :state attr_writer :lock_code attr_accessor :door_number def initialize @state ='Closed' end end door = Door.new #puts door.state ='Open' # Error as its read-only door.lock_code ='4563' # Ok #puts door.lock_code # Not Ok, as its a write-only

9) Inline instantiation with attribute hash

# Its not in-built, some custom approaches :http://stackoverflow.com/questions/2680523/dry-ruby-initialization-with-hash-argument

10) Access Control

class DoorLockprivate def reset_timer end protected def emergency_unlock end public def unlock access_code end def lock end end lock= DoorLock.new puts lock.respond_to?'unlock' # true puts lock.respond_to?'reset_timer' # false puts lock.respond_to?'emergency_unlock' # TRUE ,unlike C# whereprotected are visible only internally or to inherited clases

11) Lambda, Proc and Block

class AsyncProcessor def do_async_job arg1, call_back raise 'Sorry,it doesnt look like lambda' unless call_back.is_a? Proc # Do the job call_back.call('Status: Done') end end processor = AsyncProcessor.new processor.do_async_job("long task",lambda{|result| puts result})

12) Date Formatting

now = Time.now puts "#{now.year}-#{now.month}-#{now.day}" puts now.strftime("%a %b %d %H:%M:%S")

Viewing all articles
Browse latest Browse all 9

Trending Articles