[#382478] Understanding Ruby Inside Out — Konstantin Ka <paranox3@...>

What makes Ruby work?

13 messages 2011/05/01

[#382594] Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...>

I am trying to install/build shoes3 under Red Hat EL 6 and I am having heck

41 messages 2011/05/03
[#382595] Re: Problem installing shoes3 on RHEL 6 — Steve Klabnik <steve@...> 2011/05/03

Two things:

[#382596] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/03

Well friend, I can hardly put on a shoe. I am sure I can't build one!

[#382597] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/03

Actually, I did try to build my own shoes using the instructions:

[#382598] Re: Problem installing shoes3 on RHEL 6 — Steve Klabnik <steve@...> 2011/05/03

Hm, the last person that got that error said they were using 1.8, but you're

[#382643] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/04

Hi Steve,

[#382649] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/04

On Wed, May 4, 2011 at 3:24 PM, Ruby Student <ruby.student@gmail.com> wrote:

[#382652] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/04

Brabuhr,

[#382654] Re: Problem installing shoes3 on RHEL 6 — Steve Klabnik <steve@...> 2011/05/04

You have to then reinstall 1.9.2 after installing openssl-devel for it to

[#382658] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/04

Well gents,

[#382669] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/04

On Wed, May 4, 2011 at 2:21 PM, Ruby Student <ruby.student@gmail.com> wrote=

[#382698] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/05

brabuhr,

[#382699] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/05

> I downloaded your build.

[#382711] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/05

Brabuhr,

[#382712] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/05

On Thu, May 5, 2011 at 11:45 AM, Ruby Student <ruby.student@gmail.com> wrote:

[#382713] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/05

> Also try:

[#382715] Re: Problem installing shoes3 on RHEL 6 — Ruby Student <ruby.student@...> 2011/05/05

Here is what I get:

[#382719] Re: Problem installing shoes3 on RHEL 6 — brabuhr@... 2011/05/05

On Thu, May 5, 2011 at 2:50 PM, Ruby Student <ruby.student@gmail.com> wrote:

[#382601] Iterating over an Array of Hashes — Peter Hicks <peter.hicks@...>

All,

23 messages 2011/05/03
[#382607] Re: Iterating over an Array of Hashes — 7stud -- <bbxx789_05ss@...> 2011/05/04

Peter Hicks wrote in post #996483:

[#382609] Re: Iterating over an Array of Hashes — John Feminella <johnf@...> 2011/05/04

On Tue, May 3, 2011 at 20:34, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

[#382612] Re: Iterating over an Array of Hashes — 7stud -- <bbxx789_05ss@...> 2011/05/04

John Feminella wrote in post #996498:

[#382613] Re: Iterating over an Array of Hashes — John Feminella <johnf@...> 2011/05/04

Your example doesn't contain nested hashes, while mine does. That's

[#382616] Re: Iterating over an Array of Hashes — Christopher Dicely <cmdicely@...> 2011/05/04

On Tue, May 3, 2011 at 7:08 PM, John Feminella <johnf@bitsbuilder.com> wrote:

[#382641] Re: Iterating over an Array of Hashes — Chad Perrin <code@...> 2011/05/04

On Wed, May 04, 2011 at 12:28:18PM +0900, Christopher Dicely wrote:

[#382661] Re: Iterating over an Array of Hashes — Christopher Dicely <cmdicely@...> 2011/05/04

>>

[#382650] Creating variables on an OpenStruct with dynamic names — "Jolyon R." <jolyonruss@...>

Hey guys,

11 messages 2011/05/04

[#382686] Lets play a guessing game. (how to code this better?) — Super Goat <ruby-forum@...33mail.com>

I am a new Rubyist. I told my friend that I was learning Ruby. He asked

21 messages 2011/05/05

[#382764] rubygems-update 1.8.1 Released — Eric Hodel <drbrain@...7.net>

rubygems-update version 1.8.1 has been released!

11 messages 2011/05/06

[#382777] Ruby Activity — "Bobby S." <kajisakka@...>

Is ruby still being developed? The official site has last release in dec

25 messages 2011/05/07
[#382782] Re: Ruby Activity — Stu <stu@...> 2011/05/07

Ruby comes with tk build in. While your investigating which toolkits

[#382907] Re: Ruby Activity — "Patrick Lynch" <kmandpjlynch@...> 2011/05/10

I'm using Ruby 1.8.7 and it does not contain TK...

[#382908] Re: Ruby Activity — Eric Christopherson <echristopherson@...> 2011/05/10

On Tue, May 10, 2011 at 2:46 PM, Patrick Lynch <kmandpjlynch@verizon.net> wrote:

[#382933] Re: Ruby Activity — "Patrick Lynch" <kmandpjlynch@...> 2011/05/11

Hi,

[#382938] Re: Ruby Activity — Stu <stu@...> 2011/05/11

If you use macports this is how you enable the variant flag:

[#383075] Git configuration file: .gitconfig — "Patrick Lynch" <kmandpjlynch@...> 2011/05/12

Good morning,

[#382788] Help with while condition OR condition — "Bill W." <sirwillard42@...>

Hi everyone,

14 messages 2011/05/07

[#382795] Threading Loops — "Bobby S." <kajisakka@...>

I understood how to thread functions, but I don't understand how to

14 messages 2011/05/07

[#382903] Ruby 1.9.3 documentation challenge — Eric Hodel <drbrain@...7.net>

With the freeze of Ruby 1.9.3 coming up near the end of the month I =

17 messages 2011/05/10

[#382904] Enumerable#find returns an enumerator? — Roger Pack <rogerpack2005@...>

Hello all.

13 messages 2011/05/10

[#382913] Generate random string matching specific pattern and length — Kevin <darkintent@...>

I'm trying to generate a random set of strings to fill a database with that

10 messages 2011/05/10

[#382916] gsub and multiple-replacement — Greg Hacke <greghacke@...>

So I have a file that I am replicating per user.

11 messages 2011/05/10

[#382991] Scope problem (?) in implementing Design Patterns in Ruby — RichardOnRails <RichardDummyMailbox58407@...>

Hi,

28 messages 2011/05/11

[#383041] Learning Ruby advice needed — Rubist Rohit <passionate_programmer@...>

While learning a new language, I find it very boring to read again the

23 messages 2011/05/12
[#383050] Re: Learning Ruby advice needed — Regis d'Aubarede <regis.aubarede@...> 2011/05/12

> ...I find it very boring to read again the

[#383092] Re: Learning Ruby advice needed — Rubist Rohit <passionate_programmer@...> 2011/05/12

I don't understand how you all feel so comfortable with "irb". It is

[#383046] where does the pure method defined when starting irb — Brian Xue <brxue.cn@...>

Hello,

13 messages 2011/05/12

[#383047] RubyGems 1.8 treats warnings as errors for C extensions, fails to install RedCloth? — Quintus <sutniuq@...>

-----BEGIN PGP SIGNED MESSAGE-----

10 messages 2011/05/12

[#383048] Digest::Base problem — Martin Hansen <mail@...>

I have the following problem:

23 messages 2011/05/12

[#383093] Shell pipeline in Ruby? — Michal Suchanek <hramrach@...>

Hello,

21 messages 2011/05/12

[#383098] Jruby -v failing with possible Java version issue — Ruby Student <ruby.student@...>

Hello team,

11 messages 2011/05/12

[#383144] indenting "end" — Chad Perrin <code@...>

I've been seeing a lot of this lately:

15 messages 2011/05/12

[#383182] ruby.exe crashing on windows xp — "Glory L." <glory.lo778@...>

Hi there,

16 messages 2011/05/13

[#383271] Ruby Future Or? — Robert Johns <piratej74@...>

I was thinking today and I was wondering if ruby has a future when

34 messages 2011/05/15

[#383305] Linux utility with reverse index facility? — no.top.post@...

awk &stuff can "give me the the Nth element",

15 messages 2011/05/16

[#383306] canonical/syntax-diagrams representation. — no.top.post@...

I started investigating ruby.

19 messages 2011/05/16

[#383314] BARRIER - ruby gem - code converter not found (UTF-16LE to IBM737) — Ilias Lazaridis <ilias@...>

After visiting ruby-lang.org, I decided to get the actual 1.9.2

16 messages 2011/05/16

[#383442] Generating Functions in Ruby — Andreas Lundgren <andreas.lundgren.x@...>

Hi!

28 messages 2011/05/18

[#383476] Writing formulas to excel spreadsheet — Will James <ampclj9@...>

Hi, everyone. I've just started using ruby a couple of days ago, and

28 messages 2011/05/19
[#383544] Matz never said Microsoft was the Devil Incarnate. (or did he?) — Mike Stephens <rubfor@...> 2011/05/20

Will

[#383546] Re: Matz never said Microsoft was the Devil Incarnate. (or did he?) — Daniel Berger <djberg96@...> 2011/05/20

[#383552] Re: Matz never said Microsoft was the Devil Incarnate. (or did he?) — Will James <ampclj9@...> 2011/05/21

Daniel Berger wrote in post #999984:

[#383566] Re: Matz never said Microsoft was the Devil Incarnate. (or did he?) — Chad Perrin <code@...> 2011/05/21

On Sat, May 21, 2011 at 11:28:26AM +0900, Will James wrote:

[#383578] Re: Matz never said Microsoft was the Devil Incarnate. (or did he?) — Mike Stephens <rubfor@...> 2011/05/21

Chad Perrin wrote in post #1000098:

[#383604] Re: Matz never said Microsoft was the Devil Incarnate. (or did he?) — Chad Perrin <code@...> 2011/05/22

On Sun, May 22, 2011 at 06:14:10AM +0900, Mike Stephens wrote:

[#383528] BARRIER - ruby yaml - utf-8 characters not human readable — Ilias Lazaridis <ilias@...>

After reading within some archives, it seems that the standard-

10 messages 2011/05/20

[#383534] Object-Oriented thinking — Michael Sokol <mikaa123@...>

Hello everyone,

18 messages 2011/05/20

[#383558] Teaching Ruby in CS1 — Franck Ditter <franck@...>

Who knows some good CS1 references for teaching Python ?

12 messages 2011/05/21

[#383597] BARRIER - json, thin, eventmachine - do not install on windows — Ilias Lazaridis <ilias@...>

I use a fresh installation of ruby 1.9.2p180 to make some tests with

24 messages 2011/05/22

[#383629] Tools for Ruby code analysis — "Alex V." <alex.vpro@...>

Hello everyone,

16 messages 2011/05/23

[#383678] "Local variable within code blocks do not interfere with those outside the block" — Kaye Ng <sbstn26@...>

I read this in a book.

16 messages 2011/05/24

[#383686] Using sprintf() to print a Hash — Iñaki Baz Castillo <ibc@...>

Hi, I just have a single Hash and want to print it into the standar

12 messages 2011/05/24
[#383687] Re: Using sprintf() to print a Hash — Iñaki Baz Castillo <ibc@...> 2011/05/24

2011/5/24 I=C3=B1aki Baz Castillo <ibc@aliax.net>:

[#383712] Changes for Ruby in Debian (and Ubuntu) — Lucas Nussbaum <lucas@...>

Hi,

16 messages 2011/05/24

[#383760] Method that mutates object — jason solomon <solomon.jas@...>

Say we want to write a String method called clear that takes a given

26 messages 2011/05/25

[#383770] jruby --1.9 : Exception in thread "RubyThread-1: threadtest.rb:1" java.lang.LinkageError: loader (instance of org/jruby/util/JRubyClassLoader): attempted duplicate class definition for name: "threadtest$block_0$RUBY$true?" — Markus Fischer <markus@...>

Hi,

6 messages 2011/05/25
[#384091] Re: jruby --1.9 : Exception in thread "RubyThread-1: threadtest.rb:1" java.lang.LinkageError: loader (instance of org/jruby/util/JRubyClassLoader): attempted duplicate class definition for name: "threadtest$block_0$RUBY$true?" — Charles Oliver Nutter <headius@...> 2011/06/03

That's wacked. Please file a bug at http://bugs.jruby.org. Looks like

[#383790] CORE - Object Instantiation and Location — Ilias Lazaridis <ilias@...>

#ruby 1.9

17 messages 2011/05/26

[#383810] Need help bringing select array lines together — Paul <tester.paul@...>

Hi there, I am looking at some old, confusing ruby code that works but

12 messages 2011/05/26

[#383961] CORE - Specialized Attribute Definition — Ilias Lazaridis <ilias@...>

ruby 1.9

13 messages 2011/05/31

[#383981] What editor or IDE do you use? — Mike Hansen <skrabbit@...>

I'm pretty new to Ruby. What editor or IDE do you use? I usually use VIM

41 messages 2011/05/31
[#384018] Re: What editor or IDE do you use? — Chad Perrin <code@...> 2011/06/01

(In the following, I will use "vi" to refer to vi-like editors in

[#384021] Re: What editor or IDE do you use? — Stu <stu@...> 2011/06/01

New POLL!!!

[#384022] Re: What editor or IDE do you use? — "Wilde, Donald S" <donald.s.wilde@...> 2011/06/01

Jeez... BSD or Linux... or Doze?=20

[#384023] Re: What editor or IDE do you use? — Chad Perrin <code@...> 2011/06/01

On Thu, Jun 02, 2011 at 03:09:06AM +0900, Wilde, Donald S wrote:

[#384026] Re: What editor or IDE do you use? — Stu <stu@...> 2011/06/01

You made your point succinctly and eloquently Chad.

[#384027] Re: What editor or IDE do you use? — Xavier Noria <fxn@...> 2011/06/01

Let me add to this thread that the editors of dedicated IDEs are

Re: update variables based on own value - critique please?

From: Josh Cheek <josh.cheek@...>
Date: 2011-05-01 20:23:34 UTC
List: ruby-talk #382516
On Sun, May 1, 2011 at 3:44 AM, Peter Ehrlich <crazedcougar@gmail.com>wrote:

> Hey!
>
> First, some background.  The goal here is an experiment to make the best
> code that I can.  My challenge is with returning JSON in an ajax
> request.  In a standard http request, rails view templates would handle
> preparing the data -- pluralizing, capitalizing, whatever.  However,
> there's no view in this case.
>
> The simple approach looks like this:
>
>  def json_for_view
>    rounds['last_pushed']['c_score'] = time_ago_in_words
> DateTime.parse(rounds[:last_pushed][:c_score])
>    rounds['last_pushed']['d_score'] = time_ago_in_words
> DateTime.parse(rounds[:last_pushed][:d_score])
>    rounds['last_pushed']['c_score'] = Date.parse
> rounds[:c_score].to_s(:'January 1, 2011')
>    rounds['last_pushed']['d_score'] = Date.parse
> rounds[:d_score].to_s(:'January 1, 2011')
>    rounds.to_json
> end
>
> I see a couple of problems here:
>  - the field keys are specified first.  This is not DRY.  (etc all the
> headaches of non dry code)
>  - A nicer feel would be if there was a method applied to each value,
> like rounds[:last_pushed][:d_score].make_pretty
>
>
> Here's what I've come up with:
>
>
>
> # lib/toolbox.rb
> class Object
>  def send_updates!(hash)
>    # updates values based on blocks passed in a hash
>
>    hash.each do |key, block|
>        self[key] = block.call(self[key]) if self[key]
>    end
>
>  end
> end
>
>
> # the model file
>  def json_for_view
>    rounds = JSON.parse(self.rounds)
>
>    pushed_pretty  = lambda { |score|
> time_ago_in_words(DateTime.parse(score))    }
>    created_pretty = lambda { |score| Date.parse(score).to_s(:'January
> 1, 2011') }
>
>    rounds['last_pushed'].send_updates!({
>      'c_score' => pushed_pretty,
>      'd_score' => pushed_pretty
>    })
>    rounds['created_at'].send_updates!({
>      'c_score' => created_pretty,
>      'd_score' => created_pretty
>    })
>
>    rounds.to_json
>  end
>
>
>
> Thoughts?
>
> Is this a common problem?
>
> Is there a common solution?
>
> Am I doing something wrong which causes this to be an issue?
>
> By the way -- is there any way to have json parse return keys as
> symbols?  I think that would be nice.
>
> Cheers & Thanks,
> --Peter
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


TL;DR: Two similar lines isn't enough to warrant all the complexity you've
added :)

-----

def json_for_view
  rounds['last_pushed']['c_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:c_score])
  rounds['last_pushed']['d_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:d_score])
  rounds['last_pushed']['c_score'] = Date.parse
rounds[:c_score].to_s(:'January 1, 2011')
  rounds['last_pushed']['d_score'] = Date.parse
rounds[:d_score].to_s(:'January 1, 2011')
  rounds.to_json
end

I'm confused, it looks like you're setting rounds['last_pushed']['c_score'],
and then immediately afterwards overriding what you initially set it to
(looking below, it appears that one of these was supposed to be
rounds['created_at']). It also sounds like you are using Rails, and I think
hashes in Rails have indifferent access, meaning
rounds['last_pushed']['c_score'] is the same as
rounds[:last_pushed][:c_score], so is the mixing of symbols and strings
intentional? Or are they actually different key/value pairs? Also, I'm
confused on the bottom two lines you access rounds[:c_score] directly, but
everywhere else, its namespaced underneath rounds[:last_pushed] or
rounds[:created_at]. Is that intentional?

-----

# lib/toolbox.rb
class Object
 def send_updates!(hash)
   # updates values based on blocks passed in a hash
   hash.each do |key, block|
     self[key] = block.call(self[key]) if self[key]
   end
 end
end

I don't think its a good idea to declare public methods on Object, unless
you *really* mean that every object should have that method (which is pretty
rare). Lets say you do this a lot, and later you need to email all your
users of updates to their accounts, so you make a send_updates! method on
Object. Boom!

I think you could just make this a private method in your model. (or if it
needs to be used in several places, put it into a module and then include it
where necessary)

-----


# the model file
def json_for_view
  rounds = JSON.parse(self.rounds)

  pushed_pretty  = lambda { |score|
time_ago_in_words(DateTime.parse(score))    }
  created_pretty = lambda { |score| Date.parse(score).to_s(:'January 1,
2011') }

  rounds['last_pushed'].send_updates!({
    'c_score' => pushed_pretty,
    'd_score' => pushed_pretty
  })
  rounds['created_at'].send_updates!({
    'c_score' => created_pretty,
    'd_score' => created_pretty
  })

  rounds.to_json
end

Hmm, we went from 7 lines to 24, introduced callbacks, and made the code
generally pretty complex to follow. DRY is good, but this might be a bit
overzealous.

The real principle of DRY isn't that two lines shouldn't have similar
patterns, it's "Every piece of knowledge must have a single, unambiguous,
authoritative representation within a system". In other words, if you do
this all over the place, and then you change it, you'll have to remember
everywhere you did it, and go update that code. Instead, if you just say
what you want to happen, and let some code be responsible for doing that,
then you only have to update it in the canonical place.

Take a look at the replacement code, then. Is it DRY? No. The logic for how
to get the pushed_pretty and created_pretty is still located in
json_for_view. The only thing you've exported is how to update the hash. If
you're using this all over the place, and that logic changes, you will still
have to go update each location. In addition, there's another more general
principle you're violating here: KISS ;)

So I'd say it was better before. If you want to refactor it, find out what
the core ideas are in the code, and extract that to its own place, which can
become the canonical way to represent that task. If this is the only place
you are using it, it becomes useful as documentation. If lots of other code
is doing something similar, then all the code that would have duplicated
instead delegate to it. A single source.

As a last thought, the section in the Pragmatic Programmer about this is
really good, and it comes up in many contexts in that book. Definitely worth
the read. I think a way that I've found most useful is to think of it not as
removing duplication, but as documenting your code. I look at your code, and
I don't know what it does. So imagine you added a comment to help me figure
it out. Now imagine if you had a method with a name that resembled the
comment, and moved the code in there. With good variable and method names
and a healthy amount of delegation, comments become mostly redundant.

-----

note: when the last argument to a Ruby method is a hash, you can omit the
curly braces. ie
  rounds['created_at'].send_updates!({
    'c_score' => created_pretty,
    'd_score' => created_pretty
  })

could become
rounds['created_at'].send_updates! 'c_score' => created_pretty, 'd_score' =>
created_pretty

In This Thread