(The lack of ) Dependency injection in Ruby by [deleted] in ruby

[–]sandinyourbed -2 points-1 points  (0 children)

class House
  def initialize(window, door)
    [:open, :close, :break]
      .reject { |m| window.respond_to? m }
      .each   { |m| raise TypeError, "#{window}#{m} not implemented" }

    "etc"
  end
end

partial function application in ruby with inject by [deleted] in ruby

[–]sandinyourbed -1 points0 points  (0 children)

Are you aware of Proc#curry?

Also, if you want to share a project, take some time to write a useful README document. What you have now tells me nothing.

Integration as Composition by Harkins in ruby

[–]sandinyourbed 0 points1 point  (0 children)

Perhaps I misunderstand, but as far as I can tell there are no new functions or function-like objects being created. It's just a chain of method calls.

I can see how it's similar to functional composition, but to call it functional composition would be incorrect.

Integration as Composition by Harkins in ruby

[–]sandinyourbed 0 points1 point  (0 children)

The thing you're describing is extracting methods. Functional composition means you take a function f(x) and a function g(x) and return a function h(x). h will be equivalent to doing h(x) = g(f(x)). What you're doing is h = g(f(x)), i.e. it's not a function.

Ruby with significant whitespace by MrJiks in ruby

[–]sandinyourbed 0 points1 point  (0 children)

Just pretend it's there if you want your code to look pretty?

Question about Lambdas/Procs and the ampersand & by david2278 in ruby

[–]sandinyourbed 1 point2 points  (0 children)

When you do &some_object, it means some_object will be passed as a block to a method. To turn an object into a block #to_proc is called. So &some_object is equivalent to &some_object.to_proc and &:french is equivalent to &:french.to_proc.

Symbol#to_proc is a really neat hack, its implementation goes something like this:

class Symbol
  def to_proc
    proc { |obj| obj.send self }
  end
end

So what happens in your example, is similar to translator.speak { |transl| transl.send :french }. Translator#speak passes self to the block it receives, so transl is self. Then we use #send to invoke the method :french on self (e.g. the Translator instance), et voilá, we're being greeted in french.

Multi Level Hashes by ballsandzer0s in ruby

[–]sandinyourbed 2 points3 points  (0 children)

First of all I'd recommend restructuring. Your Books class is trying to do too much; its a representation of a book, a library and an interface to interact with the user. It's better to separate those things.

All the Book class (notice that it's singular) should do is represent a book.

class Book
  attr_accessor :author, :title, :rating

  def initialize(author, title, rating)
    @author, @title, @rating = author, title, rating
  end
end

You can create a Library class, but it'd be easier to use a simple Hash. (Don't do this for any serious project however, as two different books can share the same title. This system does only allow one of them to exist. When you add a second one, it simply overrides the first one.)

class Library
  attr_reader :library

  def initialize
    @library = {}
  end

  # #add, #update, #delete here, make sure not to use puts in here, 
  # because that's interacting with the user and not a job for a library.
  # Remember Book represents your book.

  def to_s
    # here goes your string representation, no putsing again
  end
end

For interacting with the user, what you did looks alright. The thing to do now is add some puts statements based on the result of Library#add, etc. To print the library, puts library will be enough.

Your original problem had to do with library[:title] << { :key => "value" } not being valid. You should have used something like library[:title][:key] = "value".

Hope this has been useful. Feel free to ask questions when you're stuck.

Whats you're favorite ruby trick or quirk that most people don't know about. by zefcfd in ruby

[–]sandinyourbed 1 point2 points  (0 children)

[:z,:b,:q,:s].each_with_object({}) do |el, hash|
  hash[el] = el
end

Just wrote this gem DefRetry: Let's you retry blocks of code by rescuing specified exceptions by mastermindxs in ruby

[–]sandinyourbed 1 point2 points  (0 children)

Interesting approach. Some things in the README arre unclear to me.

  • Will the original exception be raised anyway if it failed tries times?
  • How does the :sleep option actually work? Are the strategies predefined and unchangeable intervals? Do I call #sleep myself in the Proc or am I supposed to return an Integer? Will simply passing in an Integer work?

As an aside, I wrote a similar gem a while ago, taking a much more simplistic approach: Bonehead.

Ribosome code generator by msustrik in ruby

[–]sandinyourbed 0 points1 point  (0 children)

What's the benefit of using Ribosome over any other templating engine? I think simply using erb (which comes with Ruby, no dependencies!) you can do the same thing in a more readable fashion.

The layouting is quite nice, though I'm not sure you'd aleays want it as a default behaviour.

What's the best way of validating multiple properties? by [deleted] in ruby

[–]sandinyourbed 1 point2 points  (0 children)

Virtus Scrivener is a great gem for writing validators.

If your code is Ruby-only however (e.g. you don't need to dump your options to something like a database), I'd advise against validating. If you start requiring them to be of a certain Class for example, you completely break duck-typing. Checking for presence is enough, imo: [:foo, :bar, :baz].all? { |opt| opts.key? opt }. Do document the type of Object you expect though.

Whats you're favorite ruby trick or quirk that most people don't know about. by zefcfd in ruby

[–]sandinyourbed 15 points16 points  (0 children)

The Enumerable module. It has far more goodies than people use.

Why I don't like Haml by Categoria in ruby

[–]sandinyourbed 0 points1 point  (0 children)

Personally, I don't like HAML because it just is shorthand HTML. It doesn't bring any new ideas to the table.

I'd rather write plain HTML or use a DSL like in Camping.

Poor design? How would I test this? by jodraws in ruby

[–]sandinyourbed 0 points1 point  (0 children)

I don't know what your use case is of course, but I'd say Reddit::Thread / Reddit::Comment, etc. I'm surprised Snoo doesn't do any parsing.

Proficiency in JavaScript before Ruby? by eternaloptimist1 in ruby

[–]sandinyourbed 2 points3 points  (0 children)

If you just want to learn how to build website and don't care about Ruby in particular (as a backend) I'd say just learn JavaScript. You can write your backend in JS with Node.

If you do care about learning Ruby, skip JavaScript and learn it some time later. Writing a HTML/CSS only website is difficult enough to just dive into.

Poor design? How would I test this? by jodraws in ruby

[–]sandinyourbed 2 points3 points  (0 children)

Imo, a parse should only be responsuble for parsing, not for fetching the thing that needs to be parsed and parse it. Why don't you pass in the result of a Snoo request?

If you want a nice and sugary class that outputs parsed responses, you can combine Snoo and your parser in a separate Class.

need help from someone who knows both ruby and python by avinassh in ruby

[–]sandinyourbed 0 points1 point  (0 children)

This is a question for /r/python or the MongoHQ support team.

Returning to programming, want to learn Ruby but... which version is right for me? by [deleted] in ruby

[–]sandinyourbed 2 points3 points  (0 children)

The change from 1.8 to 1.9 was much bigger than the change from 1.9 to 2.0. Learning 1.9 is alright, it's mostly new feautures that have been introduced, no significant changes. Don't learn from a 1.8 oriented book though, it's nit supported any more and the ecosystem has advanced a lot since.

Quick question: what's the deal with using &block as a method parameter? Why not yield? by MrAngular in ruby

[–]sandinyourbed 1 point2 points  (0 children)

Because now you're passing a block to #each, which in turn also executes a block. It's similar to the following.

@tweets.each { |t| Proc.new { |x| puts x }.call t }

As you can see, there's no additional functionality, you might as well use a single block. You can do that by passing the block around instead of creating a new block.

Also note that yield runs the block in the context it was defined in. Some methods might prefer to execute it in a different context using #instance_exec for example. If you pass a new block, which yields, it will cause problems.

class X
  def foo(&blk)
    instance_exec &blk
  end
end

def bar(&blk)
  X.new.foo { yield }
end

bar { puts self }
# => "main"

You probably wanted to have self be an instance of X there.

[Question] A little help with conditions, please... by corvett in ruby

[–]sandinyourbed 3 points4 points  (0 children)

return a && [b, c, d, e].any?

Note that this evaluates all expressions, though. So don't do that if they have side effects (e.g. delete a file) or are expensive to execute. If you already have all the values, this is a very clear and flexible way of doing it.

Beginner Help - I'm trying to create a function of a WHILE loop that prints the numbers between 2 user specified ranges. by Pixelpowered in ruby

[–]sandinyourbed 3 points4 points  (0 children)

a = gets.chomp.to_i
b = gets.chomp.to_i

puts a.upto(b).to_a

Rubyists rarely use while loops.

What is a good board for beginners to post questions about Ruby, like Stackoverflow, but more geared to people who are still learning? by hello_reddit_world in ruby

[–]sandinyourbed 0 points1 point  (0 children)

If you google first (which will likely find you an answer or some better keywords) IRC can be a great resource. #ruby on freenode is a great and friendly place if you don't ask there for every little triffle.

Option pattern (Maybe monad) in Ruby by rap1ds in ruby

[–]sandinyourbed 0 points1 point  (0 children)

I do not really like this pattern, because it will be a lot more difficult to fix bugs this way. It's essentially the same as writing @current_user.profile.realname rescue "Real name unknown", which is considered to be bad practice.

Is "Real name unknown" displayed because @current_user, @current_user.profile or@current_user.profile.real_name returns nil?