How is "lf" special

Trying to learn Ruby. I was running some blocks of code to see what each would output. Wanted to separate the various outputs and got tired of typing" puts “\n”. So I defined a little method: def lf; puts “\n”; end. (“lf” for “line feed.”) It did nothing. Change the lf to about any other combination imaginable, and it worked as expected.

What’s special about “lf”?

1 Like

Nothing except the only indication of a line feed is a blank line. Puts a line feed character (“\n”) will output two line feeds, I.e. a double spaced.

I tried this:

def lf; puts "\n"; end

…and it worked as expected.

However if you take a closer look at your quoted code, the “quotation marks” are not really a quotation mark:

#Your code:
def lf; puts “\n”; end

I guess there should be a syntax error (difference) in your code implementation…


( BTW: You can check here how to post a code properly to forum. )

1 Like

The issue @dezmo pointed out is called “smart quotes”. If you look closely at what I just typed or your original post of your code, you will see that a different symbol is used for starting vs ending quotation marks. Those are not valid syntax for a quote in Ruby. But, we need to verify whether those are what you actually used in the code you are testing or only how the forum formatted what you typed because it was not set out to be Ruby code.

If you are actually seeing smart quotes in your test code, you need to find where on your computer they are activated and turn them off. I know how to do this on Mac, but from your profile you are on Windows.

2 Likes

Thanks for your responses, but I don’t think it has anything to do with smart quotes because changing lf to anything else (other than a reserved word, of course) allowed the little function to work properly. That is the sort of thing that will drive a novice to distraction, but c’est la vie, I guess.

Now, if I can just get a handle on the concept of self.

When I first got into AutoLISP there were two books (AutoLISP in Plain English and Inside AutoLISP) that taught by leading the would-be programmer through examples that were actually useful. Are you aware of any similar books for Sketchup’s version of Ruby? Or even for generic Ruby? I have Programming Ruby. It’s better than useless, but not by much. Something equivalent to Automate The Boring Stuff with Python would be the cat’s pajamas.

Yes, I know there is a lot of information on the web. Some of it is helpful, some not. Separating the wheat from the chaff is a PITA, but for me the real problem is that I need a textbook that I can paw through by the hour, highlight, etc.

Thanks again for your replies.

1 Like

There is an half official tutorial here: https://github.com/SketchUp/sketchup-ruby-api-tutorials

I’m not sure if you aware of this list. There is only one Sketchup specific mentioned there: the “ Automatic SketchUp ”. Be sure to treat it with care, a little outdated… and so on, but otherwise more easy to understand.

I think you are overthinking this…
There’s no need to create a separate def method.
Just set up a variable in your start up method and use that thereafter…
So…

@lf="\n"

Then later on simply add this into any strings thus…

string="onething#{@lf}anotherthing"

then your output is on two lines…
Although

string="onething\nanotherthing"

would do it without using any variable at all by using \n ??

Thanks for those! I have bookmarked both, and I look forward to studying them.

What I was doing was comparing the results from different code blocks that I took from an online tutorial. Maybe I could have modified those blocks with \n to separate the outputs, but my little throwaway function seemed like a better solution to reduce typing in the circumstances.

You might be interested to learn that it is one of your scripts that I am attempting to modify. I could just lift the relevant portion of your code and insert it in my own, but I want to understand what you did because it looks as if I might need to write other scripts occasionally.

Using a simple solution seems the best way ?
Could you post a snippet of code showing us how you were using the method for def lf()
To me it seems you’ll have more loop to jump through than the simple version ?

Okay. Please be aware that what I was trying to accomplish was to understand ‘self,’ which is a concept I have not encountered before. So I was getting these little code blocks off of the web (These two come from Self in Ruby: A Comprehensive Overview) and I had several more as well. Obviously, I wanted to separate the output from the various examples.

I was using Alexander Schreyer’s Ruby Code Editor within SketchUp. For this reply, I copy clipped from that editor. If there are smart quotes, they came from the paste operation, not from the original code. Likewise lack of indentation.

Of course, today lf works. What the heck?!

So, here we go:

# Default code, use or delete…
require “Sketchup.rb”
model = Sketchup.active_model # Open model
ent = model.entities # All entities in model
sel = model.selection # Current selection

# Show the ruby console to view any mistakes
# Get rid of these SKETCHUP_CONSOLE lines when everything works right
SKETCHUP_CONSOLE.show
SKETCHUP_CONSOLE.clear

def lf
puts “\n”
end

# class-method.rb
class Author
# Define class variable
@@name = “John Doe”

# Getter method
def self.name
    puts "Self inside class method is: #{self}"
    return @@name
end

end

puts “Author class method ‘name’ is: #{Author.name}”

lf # Here’s that line feed.

# class-instance-method.rb
class Author

# Instance method
def name
    puts "Self inside class instance method is: #{self}"
    puts "Self.class inside class instance method is: #{self.class}"
    return "John Doe"
end

end

# Define instance
author = Author.new
puts “Author class instance method ‘name’ is: #{author.name}”

lf # Here’s another

# etc., etc.

Aren’t you glad you asked?

Please read and edit your code postings:

This is also an option.
However, believe me, Dan isn’t asking you to format the text properly to encourage you. It’s much easier to see through the post if you insert a few formatting characters.

Don’t you think it looks much better, for example:

# Define instance
author = Author.new
puts “Author class instance method ‘name’ is: #{author.name}”

as your original text:

#Define instance
author = Author.new
puts “Author class instance method ‘name’ is: #{author.name}”

To achieve this, all you have to do is type 3+3 backquote (or a punctuation mark (`) ) character before and after the code like this:
image

There is a more detailed description on a link what Dan posted. I think it worth to look. You can consider it part of the learning process of programming …

…and it’s (the proper formatting) really helps others to help you. (I believe.)

I know, - myself behaving similar -, it is easier to offended and offend others during this stupid pandemic than to act rationally. Personally I will try to be polite, respectful és helpful… but sometimes I just can’t… (I act like a kid.)
:peace_symbol:
I’m sorry if I was a little off topic …

1 Like

I means, yesterday you had a syntax error… :wink: (I guess)

Be sure that the Ruby Console is also visible, to check the real output and the syntax errors of the code.
The " Done running code. Ruby says: " in Ruby Code Editor is not so “talkative”.

(With all due respect Alex’s… but personally prefer to use external editor - Notepad++ - and copy-paste the code to “native” Ruby Console)

Tell ya’ what, Monk, I’LL make it easy for me by muting YOU, and not ever seeing any of your postings, at all, ever again.

1 Like

@DanRathbun wasn’t being pedantic or intending to offend. Your hissy fit is childish.

When you paste text into a reply, unless you set it off in a ``` … ``` (or even better ```ruby … ```) block, the forum will take it as Markdown syntax. Many legitimate Ruby characters are taken as formatting commands by Markdown, and this can make a serious mess out of code you post without these guards. You can see a few of these issues if you look at the code snippets you posted earlier - for example the conversion of your strings to use smart quotes, the bold font on @@name (which the formatter takes as an attempt to ping a forum member). Greater than and less than symbols (< and >) will often get dropped or have bizarre effects on adjacent text. Unless you want to become an expert on Markdown and how it requires escaping of its key characters (as I actually did in the text above), it is easier just to use the guard blocks.

1 Like

Thank you, dezmo, for your kind response. I hate to say it, and I cannot explain why, but I actually find the original easier to read. Maybe it is akin to my preference for paragraphs to be single-spaced with a double space between paragraphs.

But speaking of better examples, how about this:

It’s not obvious why ‘lf’ refused to work for you, but maybe something in the various blocks of code you were trying redefined it.

As for ‘self,’ I find it helps to think of it in this way: …

It would help us help you if you would format your code according to these precepts because …

vs.

Here, jump through this hoop.

I see that Mr. Rathburn is going to mute me. He needn’t bother, though, because I really do not expect to post any code again. I’ll just muddle through as best I can. For that matter, I do not think it is likely that I will post anything on this forum again. I will lurk in the background learning what I can here and there.

Have a great day, dezmo, and stay away from that Covid.

2 Likes

The phrase has an irregular descention, doesn’t it?: I had an entirely justified flash of temper; you had a hissy fit; he became dangerously overwrought. :slightly_smiling_face:

Don’t let the crow catch you.