Mohamad
  • Member for 10 years, 11 months
  • Last seen more than a month ago
Setting defaults in a Ruby options hash
11 votes

For newer ruby versions Since Ruby 2.1 keyword arguments are the best option. def initialize(person:, greeting: "Hello") end Here, person is required while greeting is not. If you don't pass person,...

View answer
Towers of Hanoi
6 votes

If you use a hashmap you can replace your entire logic with a couple of lines. It also makes other parts of the program simpler. Here's a working example: select = towers[select] destination ...

View answer
Writing successive function calls
Accepted answer
6 votes

I honestly think your first version is the best one. It's readable, and makes more sense at a glance than the refactorings. That said, I would consider a couple of things. def generate violations = ...

View answer
Controller action to process orders and payments
6 votes

I would suggest using a service object. I would like to see what others think about this. It looks like there's too much going on for the controller. The controller should be responsible for ...

View answer
Towers of Hanoi
5 votes

I would like to add a refactoring using object-oriented design. This is not to say it's "better"; that's not the point. I want to show another way of thinking about the problem, and demonstrate Ruby's ...

View answer
RSpec/Capybara tests
4 votes

I think some of your expectations might lead to brittle tests. I would modify the expectations a little. For example: expect(page).to have_content('Logged in successfully') What happens if you ...

View answer
Simple DRY Rails - check if a model belongs to a user
Accepted answer
3 votes

I suggest you extract two methods: authorized? to check if the user is authorized, and not_authorized to handle behaviour. You would then end up with this: def new @model = ParentModel.find(params[:...

View answer
Snakes and ladders random world builder
Accepted answer
3 votes

I'm going to be bold and review my own code having spent the last hour refactoring it. The original implementation had bugs. Sometimes less than 10 ladders were placed. This is because the algorithm ...

View answer
Which syntax is preferred to create a Hash in Ruby on Rails?
3 votes

It depends. I prefer the newer syntax because I feel that it is more readable. As others have pointed out, however, the newer syntax is only compatible with Ruby 1.9.2+. You should be aware, however, ...

View answer
Finding the number in a list that differs in parity
2 votes

Well, one way we can do it is by counting the first three elements. If more than one is odd, it means the sequence is odd, and we should look for an even number. If one or less is odd, the sequence is ...

View answer
Decorator pattern for an application
1 votes

To expound on @papirtiger's answer re using the Delegator class, consider this meta programming... class FileBinderDecorator def method_missing(method_name, *args, &block) # ... def ...

View answer
Project Euler #13 in Swift - Large sum
1 votes

You can shorten your program tremendously. Consider the following two things: You only need to sum the first 11 numbers to get the correct sum Take advantage of Swift's functional aspect The entire ...

View answer
Tell, Don't Ask when dealing with displaying model attributes or a null replacement
Accepted answer
1 votes

I want to offer an alternative to helpers because I believe in this particular case using a presenter to isolate view logic is better than using helpers. As stated yourself you have many more ...

View answer
Ruby object oriented Snakes and Ladders implementation
1 votes

With the benefit of several days of thinking I'm going to have a bash and review my own code. To aid my understanding, I made a rough diagram of how my objects interact with each other. The Portal ...

View answer
URL resolver for Users, Lessons, and Series
1 votes

You could refactor your if block into something like this. But I don't think this is a good idea. I'll explain why below. object = objects[Lesson] || objects[Series] || objects[User] || nil As ...

View answer
Algorithm to rotate array elements in Ruby
0 votes

I was not able to get @Jerry Coffin's particular implementation to work, but I used his idea to create this similar \$O(n)\$ implementation. As he describes, we reverse the array at \$A[0..S-1]\$ and ...

View answer
Frog jumping algorithm
0 votes

Flambino's answer is great. I will add another way to solve this; although, as Flambino points out, it is a math problem and loops aren't needed. That said, you can use an Enumerator. def ...

View answer