[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami=C3=A1n M. Gonz=C3=A1lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Re: Ruby quick reference arranged in ASCII sequence?

From: tamouse mailing lists <tamouse.lists@...>
Date: 2012-12-15 23:59:39 UTC
List: ruby-talk #402231
On Sat, Dec 15, 2012 at 5:40 PM, tamouse mailing lists
<tamouse.lists@gmail.com> wrote:
> On Sat, Dec 15, 2012 at 9:26 AM, Eric Christopherson
> <echristopherson@gmail.com> wrote:
>> On Sat, Dec 15, 2012 at 7:00 AM, Old Grantonian <lists@ruby-forum.com> wrote:
>>> As a ruby beginner, I would be grateful for any links to a ruby
>>> reference that is arranged in ASCII sequence.
>> [...]
>>> For this line, I would like to find a reference that explains the
>>> following items in ASCII sequence:
>>>
>>> ?
>>> ==
>>> %s
>>> :
>>> \
>>> printf
>>>
>>> Probably all of these items would be explained in a good tutorial, but
>>> it might take hours to find them.
>>
>> You might try http://symbolhound.com to search for non-alphanumeric
>> things like those, along with the word Ruby to narrow things down.
>>
>
> I don't know where you'd find such a reference, but you could build
> one as you go.
>
> In the meantime, I know you asked for a reference, but the thing
> you're wondering about is this:
>
>     var = expr1 ? expr2 : expr3
>
> That's known as the "ternary" operator. if expr 1 is true, var gets
> the result of expr2, otherwise it gets the result of expr3. If you
> unpack this into more typical syntax, you get:
>
>     if expr1
>       var = expr2
>     else
>       var = expr3
>     end
>
> The second form is much clearer when reading code, but the first form
> saves quite a bit of space and typing.
>
> The next aspect is to look at the printf statements. These are
> actually a really non-ruby way of doing something, this looks a lot
> more like C, C++, or php.
>
> In ruby, everything (ALL THE THINGS!!) are objects. Objects are told
> what to do with their data by methods.
>
> last_name.length==0 ? printf(",") : printf("\"%s\",", last_name)
>
> So lets break this down.
>
>     last_name.length --- this is telling the object last_name to
> return it's length
>
>     == --- this is the equals comparison
>
> so
>     last_name.length == 0
>
> is asking if last_name is empty. Ruby has a better way of writing this:
>
>     last_name.empty?
>
> The empty? method simply asks exactly what the previous way was
> attempting to find out. empty? is implemented something like this:
>
>     def empty?
>       self.length == 0
>     end
>
> So why do that? Because this:
>
>     last_name.empty?
>
> is more readable than the other. It's a direct expression of what you
> want to know at that point.
>
> Now, onwards
>
>     printf(",")
>
> This starts to get into error territory. Just using this as it is is
> incorrect. Not that it would produce a run-time error -- it won't --
> however, it's a gross misunderstanding of the printf method. printf
> means "Print with formatting". If you aren't formatting anything,
> don't. Use print instead.
>
> The next part
>
>     printf("\"%s\","," last_name)
>
> is a slightly more legitimate use of printf, in that the variable
> last_name is being formatted as a quoted sting and printed.
>
> So all in all, what this
>
>     last_name.length==0 ? printf(",") : printf("\"%s\",", last_name)
>
> means, in pseudo code, is:
>
>   If the last name is empty,
>     just print a comma
>   Otherwise,
>     print the last name surrounded by double quotes
>       followed by a comma
>
> Now, that will all work. But it isn't idiomatic ruby.
>
>     print last_name.empty ? "," : "\"#{last_name}\","
>
> makes better use of ruby's expressiveness.
>
> Why learn idioms, you may ask?
>
> Simple, so you will understand the other speakers of the language you
> are trying to learn. If you write code solely by casting about for
> examples that might do what you want, and stop looking the moment you
> get close, just like your bookshelves that are all wobbly and falling
> over because you stopped looking for how to build them when you found
> a hammer and nails, your code will rot and fall over as well. And that
> might be okay.

Crikey, I forgot to explain the last bit:

the #{expr} notation inside a double quoted string means to substitute
the value of the expression at that point in the string, also known as
"interpolation". So:

    variable = "something"
    "this string includes the value of #{variable}"

would yield

    "this string includes the value of something"

You can put any valid ruby expression inside the braces:

    "this string includes the value of #{10 * 1024 / 3.1415}"

would yield

    "this string includes the value of 3259.5893681362404"

In This Thread