Tagged Questions

Ruby is a dynamic, open source programming language with a focus on simplicity and productivity.

learn more… | top users | synonyms

0
votes
0answers
31 views

Count the number of recursion [closed]

I have this algorithm that i made: def printPath(n, m) if n == 0 and m == 0 return puts 'X' end if n > 0 printPath(n-1, m) end if m > 0 printPath(n, m-1) end end ...
2
votes
1answer
66 views

Assigning variables in a block

It's common pattern when you need to assign a variable inside a block def query(sql) logger.debug "Db: Executing query #{sql}" result = nil ts = Benchmark.realtime do result = @db.exec sql ...
2
votes
2answers
112 views

Cleaner Code for custom query

Is there a cleaner way to do the following? friend_ids = [1,2,3,4,5] friendIDsQuery = "" friend_ids.each_with_index do |friend_id, index| friendIDsQuery += "SELECT id FROM test WHERE user_id = ...
5
votes
2answers
49 views

Ruby method return values - how to be more Ruby-like?

How should I make this more Ruby-like or just "better"? def password_format_is_valid?(submitted_password) #Gets regular expression for password format validation from settings and applies it ...
0
votes
2answers
42 views

(sort of) casting types in Ruby: changing class of an instance [closed]

I defined the class Rectangle: class Rectangle attr_reader :b, :h def initialize(b, h) @b = b @h = h end def area @b*@h end def to_s "Rectangle #{@b}x{@h}" end end and ...
5
votes
2answers
117 views

Which ruby style is preferred? [closed]

option 1 data = if page <= total_pages collection[((page - 1) * limit)...(page * limit)] else [] end option 2 data = [] if page <= total_pages data = collection[((page - 1) ...
2
votes
0answers
45 views

How might I make this code more DRY?

I have the following Ruby code in an RSpec file: describe "order" do before do LIST_LENGTH ||= 10 @skills = FactoryGirl.create_list(:skill, LIST_LENGTH) @developer = ...
2
votes
0answers
49 views

Needs code review for class implementation

could you please review the code for the implementation of the class (link: Ruby. Code review. Working alone on the code) require_relative 'robot' class Game def initialize # initialize values ...
0
votes
1answer
54 views

Ruby. Code review. Working alone on the code

I'm working alone on my code, trying to learn Ruby as I go. Class Robot is supposed to be instantiated with the position on a map, and whether it is facing toward a side of the map. It has a couple ...
3
votes
5answers
79 views

Terser way of generating deeply nested hash

I need to generate a Ruby hash that looks like this: { "children" => [ { "children" => [ { "children" => [ { "children" => [] } ]} ]} ]} ... for an arbitrary level of ...
6
votes
3answers
167 views

Best Ruby one-liner to generate a random alphanumeric string

This is a little arbitrary, but I'm curious what people might come up with. I want to generate a random alphanumeric string in ruby, as succinctly and efficiently as possible. ([nil]*8).map { ...
3
votes
2answers
79 views

Making a Small Program Fit Ruby Standards

I am a high-school freshman who is kinda new to Ruby, and I am doing a small project on Ruby. One of the big things that I want to get out of this project is how to follow the "Ruby standards" that ...
4
votes
2answers
72 views

Spoj's 1st problem in tdd using Ruby

I am pretty new to TDD and is following problems on spoj.pl for practice. I need help with following code; it was my first attempt. Problem: Your program is to use the brute-force approach in order ...
10
votes
4answers
163 views

Is there a more succinct way to write this Ruby function?

Just for fun, I want to try to write this Ruby function more succinctly. I imagine it can be done, I'm just not knowledgeable enough with Ruby yet to know how. Any suggestions? def tags return ...
6
votes
1answer
79 views

How to make my script nicer / more Ruby?

I wrote this little script in PHP a bit ago. It takes a .csv file and outputs a .txt file with certain data arranged in a "psuedo-array" looking format. I recently started messing around with ruby so ...

1 2 3 4 5 12
15 30 50 per page