Posts in ruby

How to Split an Array Based on a Fixed Length (Hint: You Don't Use .split)

Say you have a string that you need to split up based on a fixed length of characters (in this case, every five: "jamesjonesspiesrabidbirds". Unfortunately, the split method doesn’t help us here.

Instead, we can use .scan. This method looks for regex matches and adds them to an array, rather than taking a string and breaking it up based on a delimiter. It’s a useful method in those cases where .split just can’t handle the job.

```ruby

"jamesjonesspiesrabidbirds".scan(/.{5}/) #=> ["james", "jones", "spies", "rabid", "birds"]

```

The regex /.{5}/ stands for “exactly 5 of any character” – the . means ‘any character’, and the curly braces with one number means “exactly this amount”. The scan method adds every five characters of the string to the array it returns, to give us [“james”, “jones”, “spies”, “rabid”, “birds”].

written in in arrays, fixed length, ruby, scan, split

Monday Learnings

I learned some cool things today, which I’d like to share here.

1) The each_cons method. It’s great. While working on Project Euler #11 with a friend, he showed me this method. It is applied to an array and takes an argument for n elements – it then returns an enumerator with the different variations of 3 consecutive elements from the array. I’m not sure I’m doing it justice, so here is an example:

```ruby

array = [1,2,3,4,5,6,7,8]
array.each_cons(3).to_a 3 # => [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8]]

``` This was very helpful in finding the products of each possible set of four consecutive elements (horizontally) in the problem’s grid. Nice.

written in in ampersand, methods, pry, ruby, shortcuts Read on →

Project Euler - Pythagorean Triplets

I’ve been trying to crank through Project Euler questions over the past week or so. They are pretty challenging at first sight, but I’m finding that after looking at them for a few minutes, patterns start revealing themselves and the problem becomes much clearer. Today’s question is the following:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc

written in in math problems, project euler, pythagorean triplets, ruby Read on →

Implementing CarrierWave - a Simple Guide

This weekend I decided to add file uploads to a small project I’ve been working on (mostly to get some additional rails practice). After searching for a while, I came across Carrierwave, which is a simple ruby gem that sets you up with easy image uploading and processing.

The setup was a lot easier than I expected. First step was to add the carrierwave gem to the project’s gemfile and run ‘bundle install’. Straightforward. After this, run ‘rails generate uploader {uploader_name}’ (in my case, the name was guests (as in hotel guests). In your app file you’ll now find an ‘uploaders’ directory, in which you will find the uploader class that you just generated. Cool. Leave that there for now.

written in in carrierwave, coding, flatrion school, gem, rails, ruby, uploading Read on →