background preloader

Ruby

Facebook Twitter

Python Vs Ruby Code Examples. Moved from PythonVsRuby: (See also PythonRubyAttrComparison, PythonRubyInitializer) In RubyLanguage, every object has a method called "method_missing", which is called every time an undefined method is invoked. You can override method_missing, for super-reflective ease in creating things such as facades. Does PythonLanguage have this feature? In Python you would override the method __getattr__(self, name). From this method you can either delegate to another method or throw an exception. I think you should even override __setattr__(self,name), and maybe __getattribute__ """The code below is not a good implementation of method_missing, because it does not trap the arguments to the method""" Do you really need to do that to replicate method_missing? Class Facade(object): def __getattr__(self, what): try: return super(Facade, self).

I'm writing some code in Python and I have come across a situation where, in Ruby, I would factor out a new control structure. (untested!) [1,2,3,4].map! Answers. Python Vs Ruby. Using Ruby to Control Lego Mindstorms NXT - Tim Dysinger. Ruby Array | Jonathan Hui. Ruby Literal Array [1, 2, 3, 4, 5] # 5 elements array [a..z, A..Z] # An array of two ranges [[1,2],[3,4]] # Nested arrays [1..2, [1,2,4], 4, "val"] # Mixed type Array[1,2,3] # [1, 2, 3] [0,1]*2 # [0, 1, 0, 1] Breaking text into a Ruby array words = %w[Break this into an array of words] # ["Break", "this", ..., "words"] letters = %w|aa b c d| # ["aa", "b", "c", "d"] letters = %W(aa b c d) # ["aa", "b", "c", "d"] Instantiate an Ruby array list = Array.new list = Array.new(3) # [nil, nil, nil] list = Array.new(4){|x| x**2} # [0, 1, 4, 9] # Create a 4 element array sharing the same object list1 = Array.new(4, "a") # ["a", "a", "a", "a"] list1[0].upcase!

# ["A", "A", "A", "A"] # Create a 4 elements array with its own object list2 = Array.new(4){"a"} # ["a", "a", "a", "a"] list2[0].upcase! Initialize a nil input parameter def m1(list) list ||= [] # same as l = l.nil? A = [1,2,3] a = 1, 2, 3 # Same as above Access a Ruby array by index Ruby functions in accessing an array Ruby and, or & uniq operation.

RVM: Ruby Version Manager - RVM Basics. $ rvm help # Documentation Index Wondering why you should use RVM? For a start, not only does RVM make installing multiple ruby interpreters / runtimes easy and consistent, it provides features such as gemsets that aren't typically supported out of the box on most ruby installs. RVM also lets you use different rubies in a manner that wont mess with your existing ruby install (unless you tell it to) as well as letting you run multiple different rubies in seperate terminals concurrently! Getting started First, you must ensure that you install RVM . Post Install Configuration The rvm installation documentation instructs you to put the following line at the very end of your bash profile: [[ -s "$HOME/.rvm/scripts/rvm" ]] && .

NOTE: This is for a user install. For those who are not very familiar with bash scripting, I will explain in the next few paragraphs what the line above does. [[ condition ]] evaluates the condition inside the double brackets and returns true or false. . . $ type rvm | head -1. Deploying with JRuby. About this Book 224 pages Published: Release: P1.0 (2012-07-09) ISBN: 978-1-93435-697-5 NEW: See this video on using Torquebox for high-availability deployment on the JVM. JRuby deployments have fewer moving parts and consume less memory than traditional Ruby deployments, but to deploy your apps on the JVM, you need to learn some new approaches. This book introduces you to three JRuby deployment strategies that will give you the performance and scalability you need while letting you use the language you love.

You’ll start by porting an existing application to JRuby, preparing the app to take advantage of the JVM platform. With your environment in place, you’ll experiment with simple JRuby deployment with Warbler as you package your Ruby web application into a single file you can deploy to a Java application server. Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.

What You Need: Using the Ruby Development Tools plug-in for Eclipse. Editor's note: To learn what's new and changed in RDT since this article was written, read the developerWorks article "Easy Ruby development, the Eclipse way. " Why Ruby? Why would any self-respecting Java developer care about Ruby? Ruby is a general-purpose scripting language created 10 years ago in Japan. Contrary to popular belief, it is a pure object-oriented language. Unlike Java technology, Ruby has no scalars, so everything, including integers, are first-class objects. Ruby is also red-hot right now. The other reason to start playing with Ruby is that all the cool kids are doing it. One of the main limiting factors to widespread development in Ruby is a good development environment (for those who don't want to learn Emacs).

Back to top Setup Before you can start playing with Ruby, you have to install — or verify that you have — the Ruby interpreter and libraries, and the Ruby Development Environment. Getting Ruby If you get a version number, you are ready to go. Getting docs Getting RDT. 東 . Blog. MacRuby & Xcode 4: Build a Self-Contained MacRuby Application - The Ranger Station. MacRuby » Home. MacRuby Intro.