Posts Tagged ruby

Small things I like about ruby

The interactive ruby shell (irb) is really great tool to get comfortable with trying out features of ruby.

There are a couple of things that really help to make irb interactive. These come from ruby’s introspective capabilities.

The first is the method “methods” which returns a list of all the methods that relate to an object and since everything is an object in ruby you can call methods on anything:

For example what are the methods that an integer can take:

10.methods

which returns:

:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :to_json, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :to_bn, :singleton_method_added, :coerce, :i, :+@, :eql?, :quo, :remainder, :real?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, ...]

You can do the same on an array. For example an empty array:

[].methods

Now that is kind of useful, but as long as people have given their methods decent parameter names then you can work out how to call a method. So for example I have created a module ‘Smig’ with a method “create_imageimporter”. So if I enter the following in irb:

Smig.method(:create_imageimporter).parameters

Which returns:

[[:req, :pathToFile], [:opt, :importerName]]

This tells me that the first parameter is required and because I named the parameter pathToFile then I know I need to supply a path to a file. The second parameter is an optional parameter and is called importerName. Without reading about Smig this is probably not helpful but for me this is enough information to remind me how to call “create_imageimporter” without having to go and look up documentation elsewhere.

#Update 23 June 2014

I’ve now started using the Yard documentation tool for reference documenting of ruby code.

I’ve also been testing the use of MovingImages asynchronously but this of course means having the ability to track when asynchronous tasks are completed. A MovingImages asynchronous task can save the results of the asynchronous command to a file when it is completed. As a result I’ve installed the ruby gem ‘listen‘. Documentation for the ruby gem listen. The githug repo for listen.

Tags:

A few ruby notes

Mostly for my own benefit, but my use of ruby is intermittent enough that the fundamentals aren’t remembered as well as they should be.

Global variables are preceded with a dollar sign. “$”.

Class variables are preceded with two at signs “@@”.

Instance variables are preceded with an at sign “@”.

This blog about ruby symbols, which can be considered as immutable strings but with added usefulness is really helpful.

This blog post describes the syntax for writing class and instance methods in ruby.

To not have to precede all method calls with the name of a module you can do include ModuleName.

 

Tags:

Ruby: Hashes containing hashes

I’ve recently noticed when using Ruby Hashes that whether they store values by reference or make a copy depends on whether the hash contains another hash.

For example:

contextWidth = 256
contextHeight = 192

contextSize = { :width => contextWidth, :height => contextHeight }
puts contextSize
puts contextSize[:width]

contextWidth = 320
puts contextSize
puts contextSize[:width]
# contextSize[:width] remains unchanged.

If I however make a hash that contains other hashes then the behaviour changes.

contextWidth = 256
contextHeight = 192
contextSize = { :width => contextWidth, :height => contextHeight }

contextLeft = 10
contextBottom = 5
contextOrigin = { :x => contextLeft, :y => contextBottom }
contextRect = { :origin => contextOrigin, :size => contextSize }

puts contextRect

contextOrigin[:x] = 20 # was 10.

puts contextRect
puts contextRect[:origin][:x]
# contextRect[:origin][:x] changes

It seems Ruby containers set objects by value when the object is a simple type, but if the object is more complex like another container then ruby sets the value by reference. This is using ruby 2.0.0p247 on OS X 10.9.

An addendum. If you want to remove key/value pair from a hash and return the original hash with the key/value removed not a new hash there is this ruby hash stack overflow discussion. The solution that looks the simplest for non ruby/rails developers is:

h = {:a => 1, :b => 2}
 => {:a=>1, :b=>2} 
h.tap { |hs| hs.delete(:a) }
 => {:b=>2} 

Tags:

Why Ruby?

I’ve come to the realisation that unix shell scripts though workable are not really the way to go with driving the MovingImages LaunchAgent and I needed an alternative scripting environment. It very quickly came down to python or ruby. I found it very difficult to choose between the two. In the end it was that learning ruby provides an easier path to ruby-motion if I choose to in the future. Amongst the ruby/python wars on the internet I found these quotes from Python versus Ruby.

Python: “There’s a most understandable way to do something and that is how it should be done”

Ruby: “Inherits perl’s philosophy of having more than one way to do the same thing”

Now I kind of like Python’s philosophy of trying to keep things simple by choosing one way of doing something, that you believe to be the most understandable way and that is what you implement. It should keep the language compact and relatively simple but I became concerned as to who decides what is the most understandable way of doing something. We don’t all think the same way.

But I also don’t like the idea of inheriting perl’s philosophy. I’ve alway felt that perl is a language used by coders who like to write code that is obtuse & hard to follow as if the coders intention is to impress by obscurity. Whilst I’ve seen this style of coding in some of the ruby code I’ve looked at, I’ve also seen that because of ruby’s flexibility it doesn’t have to be this way, that there is plenty of code written for clarity.

But if you want me to help you make this decision, I’m sorry I couldn’t do it for myself.

 

Tags: , ,