Feeds:
Posts
Comments

Posts Tagged ‘ruby’

Often one must traverse a list or array of items to find and return the first instance with a certain property, attribute or id. For pattern matching in strings or texts, one would use regular expressions. For object matching in lists or arrays, usually one would write a function like this:

result = nil
instances.each do |instance|
[...]

Read Full Post »

As a dynamic script language, Ruby provides a few different options to evaluate a string or a block of code: eval, class_eval, module_eval, and instance_eval. What is the difference between them? This article describes it well. The main different is the context: instance_eval evaluates in the context of a certain instance, module_eval (and class_eval) in [...]

Read Full Post »

DSL in Ruby

A Domain Specific Language (DSL) is a computer language targeted to a particular kind of problem or application domain. There are various ways to implement a DSL in Ruby: classic blocks, instance_eval or mixins and extend. Most often instance_eval is used. How does it work? First one has to define the DSL itself, for example [...]

Read Full Post »

Understanding Ruby blocks, Procs and methods is not easy for a Ruby beginner, especially if Procs and Lambdas are involved. Yet the basic elements are simple, as Matz says, blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function.
An ampersand in [...]

Read Full Post »

Nil or Empty

Do you use statements like

.. if !name.nil? && !name.empty?
.. if params[:name] and !params[:name].empty?

in your code? Using the blank? function of Rails, they can be simplified to

.. if !name.blank?
.. if !params[:name].blank?

But we can go even further. Using the present? function, this is equal to

.. if name.present?
.. if params[:name].present?

See the documentation [...]

Read Full Post »

The splat operator in Ruby, Groovy and Perl allows
you to switch between parameters and arrays:
it splits a list in a series of parameters,
or collects a series of parameters to fill an array.
The split mode turns an array into multiple-arguments:

>> duck, cow, pig = *["quack","mooh","oing"]
=> ["quack","mooh","oing"]

The collect mode turns multiple-arguments in an array:

>> *farm = duck, [...]

Read Full Post »

Date Arithmetic

Date arithmetic in Ruby is possible because Ruby’s time objects are stored internally as numbers. Additions to dates and differences between dates are handled by adding to and subtracting the underlying numbers. Rails offers nice ‘ago’ and ‘from_now’ functions in ActiveSupport that allow computing lengths of time in a more human-readable way:

Date.today [...]

Read Full Post »

Using FTP in Ruby

Recently I made a post about Web Services in Ruby. It is of course also possible to get data in the traditional, old-fashioned way through FTP, and it is just as simple to upload/download files to/from an FTP server as using a Web Service. Just use the standard Net::FTP class (see also recipe 14.8 [...]

Read Full Post »

Using Web Services in Ruby

It is simple to use a web service in Ruby using the SOAP RPC Driver in the Ruby standard library. The following example is based on the Ruby Cookbook (Recipe 16.4 Writing a SOAP Client), and works well with web services created by Apache Axis. The free web service from BookPrice.com calculates an ISBN-13 or [...]

Read Full Post »

Test vs. Type

Is correctness or verification a question of test vs. type (because dynamic languages as Python or Ruby without strong typing can not be relied on to create large programs) ? It is true that testing is especially important for dynamic languages, and sometimes neglected for traditional, strongly typed languages as C# or Java. I think [...]

Read Full Post »

Sometimes it is necessary to change or to return multiple parameters in method. While passing a parameter by reference is a bit tricky in Ruby, returning multiple values from a method is simple. One can return for instance a hash, or simply write

return true, 42

which is equivalent to returning the results [...]

Read Full Post »

The modules and mixins in Ruby are a good way to implement AOP in Ruby. Typical AOP examples are debugging and logging. Modules allow you to weave in aspects of code in different classes – exactly what you need for AOP. Ruby offers full support: “extend” is used to add methods from modules as class [...]

Read Full Post »

Ruby, Ruby, Ruby..

The Ruby community is cool, isn’t it ? 8-)

Read Full Post »

There are a couple of functions which you have to look up in any
language again and again, because they are slighty different in
each language, especially those for formatting of dates and
floating point numbers.
To display dates and datetimes in different formats in Ruby on
Rails applications, the following list for default formats
may help. The default date formats [...]

Read Full Post »

If you want to tackle challenging tasks, for example climbing a big mountain, you have the choice: either you join a large climbing team and use a lot of expensive equipment, or you try it alone using free climbing.
In the software world, the task is the software to be written, the climbing equipment are the [...]

Read Full Post »

Older Posts »